You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在分类X轴上减小Matplotlib柱状图的柱宽?

调整Matplotlib单值柱状图柱子宽度的方法

要解决单值柱状图柱子过粗的问题,核心是在ax.bar()方法中通过width参数控制柱子宽度,数值越小柱子越细。

修改后的完整代码如下:

import matplotlib.pyplot as plt

# 柱状图数据
x = ['Category']
y = [10]

# 创建画布和坐标轴
fig, ax = plt.subplots(figsize=(4, 4))

# 设定柱子宽度,数值越小柱子越细
bar_width = 0.3  

# 绘制柱状图并指定宽度
ax.bar(x, y, width=bar_width)

# 显示图形
plt.show() 

调整后的效果:
柱状图效果展示

内容的提问来源于stack exchange,提问作者Naama Shenberger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 09:12:37