Here is the replication code:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
fig = make_subplots(
rows=1, cols=2,
subplot_titles=('Fig1', 'Fig2')
)
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y1': [2, 4, 6, 8, 10],
'y2': [3, 5, 7, 9, 11]
})
color = {"y1": "blue", "y2": "red"}
fig.add_trace(
go.Scatter(x=df['x'], y=df['y1'], mode='lines+markers', name='y1', line=dict(color=color['y1'])),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=df['x'], y=df['y2'], mode='lines+markers', name='y2', line=dict(color=color['y2'])),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=df['x'], y=df['y1'], mode='lines+markers', name='y1', line=dict(color=color['y1'])),
row=1, col=2
)
fig.add_trace(
go.Scatter(x=df['x'], y=df['y2'], mode='lines+markers', name='y2', line=dict(color=color['y2'])),
row=1, col=2
)
fig.show()
Here is the output figure:
We can see the legend that there are duplicates! Legend for y1 and y2 are the same in these two subplots therefore they should appear once. How to avoid such duplicates? i.e. I should have the following legend: