1

I am still designing what kind of plot with subplots I want to do but when we see the example in the documentation Multiple Subplots with Titles we have

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

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

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.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800]),
              row=2, col=1)

fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000]),
              row=2, col=2)

fig.update_layout(height=500, width=700,
                  title_text="Multiple Subplots with Titles")

fig.show()

which gives

plot with subplots

which is fine but notice that there is only one place for the legends.(trace 0, trace 1.etc)

In my design the upper left plot has one legend and the other three share some other legend . Is there a way to individualize or customize the legends of subplots?

Derek O
  • 16,770
  • 4
  • 24
  • 43
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

3

This topic was discussed on the plotly forum here, and it seems that multiple legends aren't possible in plotly. However, in the same thread @Jaydeep Mistry gives a partial workaround.

He uses legend groups to group traces together, then uses the parameter legend_tracegroupgap in the update_layout method to give the legend the appearance of being more than one legend. However, this spacing only works vertically so your multiple legends will still be vertically spaced apart on the right side of the plot. For example:

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

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

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

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

fig.add_trace(go.Scatter(x=[300, 400, 500], y=[600, 700, 800], legendgroup = '2'),
              row=2, col=1)

fig.add_trace(go.Scatter(x=[4000, 5000, 6000], y=[7000, 8000, 9000], legendgroup = '2'),
              row=2, col=2)

fig.update_layout(height=500, width=700,
                  title_text="Multiple Subplots with Titles",
                  legend_tracegroupgap=180)

fig.show()

enter image description here

Alternatively, you could add another legend by using an annotation to draw a box with the accompanying text, but it wouldn't have any functionality.

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • Thanks for the answer. It looks nice. I notice though that the legend 2 can not have the traces hidden or shown as normally. When clicking on legend 2 all traces (1,2,3) are hidden or selected together.... – KansaiRobot Feb 06 '22 at 05:49
  • 1
    @KansaiRobot my solution allows for the appearance of the legends to look as desired, but the functionality will not allow you to have individual control over traces that are in the same legend group. i think the only true solution would be to build multiple legends with dash using callbacks – Derek O Feb 07 '22 at 16:07