Plotly:如何使用分组图例设置多个子图? [英] Plotly: How to set up multiple subplots with grouped legends?
问题描述
对于每个子图,我有 3 条单独的线:2017 年、2018 年和 2019 年有 3 次go.Scatter",每个子图代表一个国家(25 个国家),始终是这 3 年.我可以使用 subplot 示例代码,但是所有 75 个图例 (25 X 3) 将全部用不同的颜色放在一起,而且很乱.
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.
我不需要在不同的子图上使用不同的颜色,我可以在所有子图的 3 年中只使用 3 种不同的颜色和 3 个图例,如果我单击例如 2017 年所有 2017 年的曲线/线消失,那将是理想的跨越 25 个子图.
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.
谁能分享一个示例代码?出于说明目的,它可以是 2 而不是 25.我在 Plotly 网站上找不到这个示例代码.
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.
这是一个示例代码:
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")
我希望只有 2 个图例:2017 年和 2018 年,而不是 6 个图例,如果所有 2017 年沿 3 个子图的颜色相同,则更容易
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
推荐答案
legendgroup
和 showlegend
的正确组合应该可以解决问题.通过下面的设置,所有 2017
跟踪都分配给相同的 legendgroup="2017"
.并且除第一个之外的所有 2017
跟踪都有 showlegend=False
.当然,对于 2018
跟踪也是如此.试试看!
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!
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()
这篇关于Plotly:如何使用分组图例设置多个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!