Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

python - Plotly: How to set up multiple subplots with grouped legends?

for each subplot I have 3 seperate line:2017 ,2018 and 2019 with 3 times "go.Scatter", each subplot represents one country (25 countries) with always these 3 years. I can use the subplot sample code but then all the 75 legends (25 X 3) will be all together with different colors and it's messy.

I don't need different colors amont different subplot, I can just have 3 different colors and 3 legends for the 3 years on all subplots, would be ideal if I click on for example 2017 that all the 2017 curve/line dissappear across the 25 subplots.

Anyone can share a sample code? it can be 2 instead of 25 for illustration purpose. I fail to find this sample code on Plotly website.

Edit: this is a sample code:

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

fig = make_subplots(rows=3, cols=1)

fig.add_trace(go.Scatter(
    x=[3, 4, 5],
    y=[1000, 1100, 1200],name="2017",
), row=1, col=1)

fig.add_trace(go.Scatter(
    x=[2, 3, 4],
    y=[1200, 1100, 1000],name="2018",
), row=1, col=1)


fig.append_trace(go.Scatter(
    x=[2, 3, 4],
    y=[100, 110, 120],name="2017",
), row=2, col=1)

fig.append_trace(go.Scatter(
    x=[2, 3, 4],
    y=[120, 110, 100],name="2018",
), row=2, col=1)

fig.append_trace(go.Scatter(
    x=[0, 1, 2],
    y=[10, 11, 12],name="2017",
), row=3, col=1)

fig.append_trace(go.Scatter(
    x=[0, 1, 2],
    y=[12, 11, 10],name="2018",
), row=3, col=1)

fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
offline.plot(fig,filename="subplots.html")

I wish to have only 2 legends: 2017 and 2018, instead of 6 legends, easier if all the 2017 has same color along the 3 subplots

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A correct combination of legendgroup and showlegend should do the trick. With the setup below, all 2017 traces are assigned to the same legendgroup="2017". And all 2017 traces except the first have showlegend=False. And of course the same goes for the 2018 traces. Give it a try!

Plot

enter image description here

Complete code

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

fig = make_subplots(rows=3, cols=1)

fig.add_trace(go.Scatter(x=[3, 4, 5], y=[1000, 1100, 1200],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)

fig.add_trace(go.Scatter(x=[2, 3, 4], y=[1200, 1100, 1000],
                         name="2018",legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)


fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False),
              row=2, col=1)

fig.append_trace(go.Scatter(x=[2, 3, 4], y=[120, 110, 100],
                            name="2018", legendgroup="2018",
                            line=dict(color='red'),
                            showlegend=False),
                 row=2, col=1)

fig.append_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12],
                            name="2017", legendgroup="2017",
                            line=dict(color='blue'),
                            showlegend=False),
                 row=3, col=1)

fig.append_trace(go.Scatter(x=[0, 1, 2], y=[12, 11, 10],
                            name="2018", legendgroup="2018",
                            line=dict(color='red'),
                            showlegend=False),
                 row=3, col=1)

fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
#offline.plot(fig,filename="subplots.html")
fig.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...