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

Matplotlib绘制柱状图出现条形宽度不均问题求助

解决条形图宽度显示不均的问题

你的问题出在子图尺寸过小,设置的条形宽度(width=0.8)在有限的子图空间里无法被精确渲染,导致视觉上看起来宽度不一致。

给你几个可行的解决办法:

  • 增大整个画布的尺寸,让每个子图有足够空间渲染:
    fig = plt.figure(figsize=(12, 12))  # 可根据需求调整画布大小
    ax = plt.subplot(4, 4, 1)
    ax.bar(np.arange(40), np.arange(40), width=0.8)
    plt.tight_layout()  # 自动调整子图间距,避免元素重叠
    
  • 减小条形宽度,适配小尺寸子图:
    fig = plt.figure()
    ax = plt.subplot(4, 4, 1)
    ax.bar(np.arange(40), np.arange(40), width=0.5)  # 缩小宽度值
    plt.tight_layout()
    
  • 隐藏部分x轴刻度,减少视觉拥挤:
    fig = plt.figure()
    ax = plt.subplot(4, 4, 1)
    ax.bar(np.arange(40), np.arange(40), width=0.8)
    ax.set_xticks(np.arange(0, 40, 5))  # 仅显示间隔为5的刻度
    plt.tight_layout()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:42:03