如何使Plotly Python子图具有相同的颜色和相应数据的图例?

huangapple go评论94阅读模式
英文:

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()

如何使Plotly Python子图具有相同的颜色和相应数据的图例?

英文:

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()

如何使Plotly Python子图具有相同的颜色和相应数据的图例?

答案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()

如何使Plotly Python子图具有相同的颜色和相应数据的图例?

huangapple
  • 本文由 发表于 2023年6月13日 15:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定