Plotly水平条形图宽度无法调整问题求助
无法调整Plotly水平条形图宽度的问题
无论我尝试何种操作,均无法调整Plotly水平条形图的宽度。以下为我当前使用的代码及输出:
fig = go.Figure(go.Bar( x=top_10_belg['ID'], y=top_10_belg['Description'], marker=dict(color='rgba(50, 171, 96, 0.6)', line=dict(color='rgba(50, 171, 96, 1.0)',width=1)) ,orientation='h')) fig.update_layout( title='Top 10 Rule Breaks by ID', height = 500, width = 400, yaxis=dict( showgrid=False, showline=False, showticklabels=True ), xaxis=dict( zeroline=False, showline=False, showticklabels=True, showgrid=False, ),barmode='group', bargap=0.4,bargroupgap=0.0, paper_bgcolor='white', plot_bgcolor='white') fig.update_yaxes(ticksuffix = " ") fig.update_yaxes(autorange="reversed") fig.show()
因保密需求,我已对标签进行遮挡,输出截图如下:
解决方法
针对水平条形图的宽度调整,可通过以下几种方式实现:
- 调整空隙参数:你当前设置的
bargap=0.4会让条形间空隙过大,导致条形偏窄。减小该值(比如设为0.1),同时保持bargroupgap=0,就能让条形视觉上更宽。 - 添加单条宽度控制:在
go.Bar()中直接加入width参数(取值0-1),比如width=0.8,这个参数控制水平条形在y轴方向的高度,对应视觉上的宽度感知。 - 增大图表整体高度:当前图表高度设为500,提升高度(比如改为600)会给每个条形分配更多垂直空间,间接让条形看起来更宽。
修改后的示例代码:
fig = go.Figure(go.Bar( x=top_10_belg['ID'], y=top_10_belg['Description'], marker=dict(color='rgba(50, 171, 96, 0.6)', line=dict(color='rgba(50, 171, 96, 1.0)',width=1)), orientation='h', width=0.8 # 控制条形的垂直高度(对应水平图的宽度感知) )) fig.update_layout( title='Top 10 Rule Breaks by ID', height=600, # 增大图表高度 width=400, yaxis=dict( showgrid=False, showline=False, showticklabels=True ), xaxis=dict( zeroline=False, showline=False, showticklabels=True, showgrid=False, ), barmode='group', bargap=0.1, # 减小条形间空隙 bargroupgap=0.0, paper_bgcolor='white', plot_bgcolor='white' ) fig.update_yaxes(ticksuffix=" ") fig.update_yaxes(autorange="reversed") fig.show()
内容的提问来源于stack exchange,提问作者work_python
相关产品推荐
相关产品推荐

