英文:
How to make plotly python subplots have the same color and legend for the corresponding data?
问题
我的问题是,我有这个图,不同子图上的相同轨迹会有不同的颜色和单独的图例。颜色问题可以通过手动覆盖来轻松解决,但这并不能解决图例问题。我之所以这样表达问题,是因为感觉应该有一个同时解决这两个问题的解决方案。
代码:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1)
# 添加一些数据
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[1,2]),
row=1, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[3,1]),
row=1, col=1)
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[2,3]),
row=2, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[1,4]),
row=2, col=1)
fig.update_layout(height=600, width=400, showlegend=True)
fig.show()
英文:
My problem is that I have this plot, and the traces with the same on different subplots will have a different color and a separate legend. The color issue can be solved easily by manually overwriting them, but that does not solve the legend. I phrased the question such, because it feels like there is a solution that should take care of both at the same time.
Code:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1)
# add some data
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[1,2]),
row=1, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[3,1]),
row=1, col=1)
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[2,3]),
row=2, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[1,4]),
row=2, col=1)
fig.update_layout(height=600, width=400, showlegend=True)
fig.show()
答案1
得分: 1
以下是您要翻译的代码部分:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1)
# add some data
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[1,2], marker=dict(color='blue'), legendgroup="blue"),
row=1, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[3,1], marker=dict(color='red'), legendgroup="red"),
row=1, col=1)
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[2,3], marker=dict(color='blue'), legendgroup="blue", showlegend=False),
row=2, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[1,4], marker=dict(color='red'), legendgroup="red", showlegend=False),
row=2, col=1)
fig.update_layout(height=600, width=400, showlegend=True)
fig.show()
希望这有所帮助。
英文:
You have to manually define color marker=dict(color='red')
, the legend group legendgroup="red"
, and implicitly say that you don't want the legend to be displayed on the second appearance for each color showlegend=False
(you need to have them present at least once if you want to appear correctly, and you have multiple of each color otherwise).
The corrected code:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=1)
# add some data
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[1,2], marker=dict(color='blue'), legendgroup="blue"),
row=1, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[3,1], marker=dict(color='red'), legendgroup="red"),
row=1, col=1)
fig.add_trace(go.Bar(name="blue", x=["a","b"], y=[2,3], marker=dict(color='blue'), legendgroup="blue", showlegend=False),
row=2, col=1)
fig.add_trace(go.Bar(name="red", x=["a","b"], y=[1,4], marker=dict(color='red'), legendgroup="red", showlegend=False),
row=2, col=1)
fig.update_layout(height=600, width=400, showlegend=True)
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论