英文:
Plotly express barmode overlay legend does not match bar/marker opacity
问题
我正在使用Plotly Express制作分面条形图,并叠加条形。图形看起来符合我的要求,但图例中的颜色与图形中显示的叠加透明度不匹配。也就是说,图形中的颜色有些透明,而图例中的颜色完全不透明。我理解重叠部分的颜色是不同的,但不重叠的部分也与图例不匹配。
import pandas
import plotly.express as px
df = pandas.DataFrame({'group':['first', 'first', 'second', 'second', 'comb', 'comb', ],
'month':[1,2,1,2,1,2],
'prop1':[.3,.4,0,0,.3,.4],
'prop2':[0,0,.5,.3,.5,.3]})
fig = px.bar(df, x="month", y=["prop1", "prop2"],
barmode="overlay", facet_col='group', facet_col_wrap=3)
fig.show()
英文:
I'm making facet bar plots using plotly express and overlaying the bars. The graphs look as I want them to, but the legend colors don't match the overlay opacity shown in the graphs. That is, the colors in the graph are somewhat transparent and the colors in the legend are completely opaque. I understand that the parts that are overlapping are a different color, but the non-overlapping also doesn't match the legend
import pandas
import plotly.express as px
df = pandas.DataFrame({'group':['first', 'first', 'second', 'second', 'comb', 'comb', ],'month':[1,2,1,2,1,2],
'prop1':[.3,.4,0,0,.3,.4],
'prop2':[0,0,.5,.3,.5,.3]})
fig = px.bar(df, x="month", y=["prop1", "prop2"],
barmode="overlay", facet_col='group', facet_col_wrap=3)
fig.show()
答案1
得分: 1
以下是您提供的内容的翻译:
到目前为止,透明度似乎不会在条形图叠加模式下更改为图例的颜色。因此,我使用图形对象复制了等效的图形。图形对象可以在图形规范中指定透明度。我为每个子图配置指定了opacity=0.5,并添加了与子图标题叠加的单个图例项。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
colors = px.colors.qualitative.Plotly
fig = make_subplots(rows=1, cols=3, subplot_titles=['group=first','group=second','group=comb'])
for i,gr in enumerate(df['group'].unique()):
dff = df.query('group == @gr')
fig.add_trace(go.Bar(x=dff['month'],
y=dff['prop1'],
name='prop1',
marker=dict(color=colors[0]),
opacity=0.5),
row=1, col=i+1)
fig.add_trace(go.Bar(x=dff['month'],
y=dff['prop2'],
name='prop2',
marker=dict(color=colors[1]),
opacity=0.5),
row=1, col=i+1)
fig.update_layout(barmode='overlay')
names = set()
fig.for_each_trace(
lambda trace:
trace.update(showlegend=False)
if (trace.name in names) else names.add(trace.name))
fig.show()
英文:
As far as I've tried, the transparency doesn't seem to change to the color of the legend in the bar graph overlay mode. So I reproduced the equivalent graph with a graph object. The graph object can specify transparency in the graph specification. I specified opacity=0.5 for each of the subplot configurations and a single legend item that overlaps with the addition of the subplot title.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
colors = px.colors.qualitative.Plotly
fig = make_subplots(rows=1, cols=3, subplot_titles=['group=first','group=second','group=comb'])
for i,gr in enumerate(df['group'].unique()):
dff = df.query('group == @gr')
fig.add_trace(go.Bar(x=dff['month'],
y=dff['prop1'],
name='prop1',
marker=dict(color=colors[0]),
opacity=0.5),
row=1, col=i+1)
fig.add_trace(go.Bar(x=dff['month'],
y=dff['prop2'],
name='prop2',
marker=dict(color=colors[1]),
opacity=0.5),
row=1, col=i+1)
fig.update_layout(barmode='overlay')
names = set()
fig.for_each_trace(
lambda trace:
trace.update(showlegend=False)
if (trace.name in names) else names.add(trace.name))
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论