this is because when you first create the figure via plotly.express
it only contains one trace. By default plotly only shows the traces in the legend if there is are at least 2 traces. For the first trace the showlegend
attribute is automatically set to False
.
For the add_scatter()
method on the other hand the the showlegend
attribute is automatically set to True
for the new trace since adding a trace implies at least 2 trace are present. But the first trace properties are unchanged, hence the lack of legend entry.