如何在Plotly堆叠柱状图中设置单侧误差棒?
问题:将Plotly堆叠柱状图的双向误差棒改为单侧
我使用Plotly绘制了堆叠柱状图,当前图表的误差棒为双向样式,希望将其修改为单侧误差棒(效果参考经Illustrator修改后的图表)。
当前实现代码
import plotly.graph_objects as go y1_values = [5, 4, 4, 5, 6, 5] y2_values = [9, 8, 7, 6, 5, 4] y3_values = [4, 6, 7, 7, 7, 9] y1_errors = [0.5, 0.6, 0.4, 0.7, 0.5, 0.8] y2_errors = [0.4, 0.3, 0.5, 0.7, 0.3, 0.5] y3_errors = [0.6, 0.2, 0.3, 0.4, 0.4, 0.4] fig = go.Figure() # Bars for increase with error bars fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y3_values, name="Increase", marker_color='#FF6692', error_y=dict(type="data", array=y3_errors, thickness=2, width=10), base=[sum(x) for x in zip(y1_values, y2_values)] ) # Bars for decrease with error bars fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y2_values, name="Decrease", marker_color='#19D3F3', error_y=dict(type="data", array=y2_errors, thickness=2, width=10), base=y1_values ) # Bars for unchanged with error bars fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y1_values, name="Unchanged", marker_color='#00CC96', error_y=dict(type="data", array=y1_errors, thickness=2, width=10), base=[0] * 6 ) # Annotation for bar1 and bar2 fig.add_annotation(text="10 minutes", xref="paper", yref="paper", x=0.07, y=-0.18, showarrow=False, font_size=20 ) # Annotation for bar3 and bar4 fig.add_annotation(text="30 minutes", xref="paper", yref="paper", x=0.5, y=-0.18, showarrow=False, font_size=20 ) # Annotation for bar5 and bar6 fig.add_annotation(text="60 minutes", xref="paper", yref="paper", x=0.93, y=-0.18, showarrow=False, font_size=20 ) # Layout fig.update_layout(barmode="stack", showlegend=True, template='plotly_white', height=700, width=1000, legend=dict(title='', orientation="h", traceorder='normal', x=0.46, y=1.05, bgcolor='rgba(0,0,0,0)', bordercolor='rgba(0,0,0,1)', borderwidth=0, font_size=20 ) ) fig.update_yaxes(showline=True, showgrid=False, linewidth=0.5, linecolor='black', title='<b>Percent</b>', titlefont=dict(size=24), title_standoff=30, ticks='outside', dtick=2, ticklen=10, tickfont=dict(size=20), range=[0, 20] ) fig.update_xaxes(title='', tickvals=[1, 1.5, 2.2, 2.7, 3.4, 3.9], ticktext=["<b>Cat</b><br>n = 15", "<b>Dog</b><br>n = 15", "<b>Cat</b><br>n = 15", "<b>Dog</b><br>n = 15", "<b>Cat</b><br>n = 15", "<b>Dog</b><br>n = 15"], ticks="", tickfont_size=20, linecolor="black", linewidth=1 ) fig.show()
当前双向误差棒效果

期望单侧误差棒效果

解决方案
要实现单侧误差棒,只需在每个error_y参数中添加symmetric=False,并将arrayminus设为全0数组,这样误差棒就只会向上延伸,不会向下。修改后的核心代码如下:
# 修改后的Increase柱子 fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y3_values, name="Increase", marker_color='#FF6692', error_y=dict(type="data", array=y3_errors, arrayminus=[0]*6, symmetric=False, thickness=2, width=10), base=[sum(x) for x in zip(y1_values, y2_values)] ) # 修改后的Decrease柱子 fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y2_values, name="Decrease", marker_color='#19D3F3', error_y=dict(type="data", array=y2_errors, arrayminus=[0]*6, symmetric=False, thickness=2, width=10), base=y1_values ) # 修改后的Unchanged柱子 fig.add_bar(x=[1, 1.5, 2.2, 2.7, 3.4, 3.9], y=y1_values, name="Unchanged", marker_color='#00CC96', error_y=dict(type="data", array=y1_errors, arrayminus=[0]*6, symmetric=False, thickness=2, width=10), base=[0] * 6 )
完整代码只需替换上述三个add_bar部分即可,其他代码保持不变,生成的图表将与期望的单侧误差棒效果一致。
内容的提问来源于stack exchange,提问作者hans
相关产品推荐
相关产品推荐

