Associating subplots legends with each subplot and formatting subplot titles

Legends per subplots are not possible unfortunately but you can emulate them with annotations, see the example below. As for subplot titles, they are annotations themselves (you can notice this by doing โ€œprint(fig)โ€ to inspect the structure of the figure) so you can tune them with fig.update_annotations.

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
    rows=1, cols=2,
    subplot_titles=("Plot 1", "Plot 2"))

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
              row=1, col=1)

fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
              row=1, col=2)


fig.update_layout(title_text="Multiple Subplots with Titles",
                  showlegend=False
                 )
fig.update_annotations(dict(font_size=8))
for col in [1, 2]:
    fig.add_annotation(dict(x=col / 2 - 0.4, y=0.8, xref="paper", yref="paper", 
                            text='trace %d' %col, showarrow=False))
fig.show()

1 Like