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

如何在Matplotlib分组柱状图的组内柱子间添加间距?

如何在Matplotlib分组柱状图的组内柱子间添加间距?

我当前使用以下代码绘制Matplotlib分组柱状图:

import matplotlib.pyplot as plt
import numpy as np
def create_grouped_bar_chart():
    # Daten für die Balken
    groups = 3
    bar_width = 0.35
    x = np.arange(groups)
    values1 = [2, 3, 4]  # Werte für die ersten 2 Balken
    values2 = [4, 5, 2]  # Werte für die nächsten 2 Balken

    # Balkendiagramm erstellen
    fig, ax = plt.subplots()
    
    # Ersten Balken jeder Gruppe mit Streifen versehen (hatch)
    bar1 = ax.bar(x, values1, bar_width, label='Beschriftung 1', hatch='///', edgecolor='black', color='white')
    bar2 = ax.bar(x + bar_width, values2, bar_width, label='Beschriftung 2', edgecolor='black', color='gray')

    # Achsenbeschriftungen
    plt.xlabel('X-Achse')
    plt.ylabel('Y-Achse')
    
    # Achsenbereiche festlegen
    plt.ylim(0, 6)
    plt.yticks(np.arange(0, 7, 1))

    # X-Achse beschriften
    ax.set_xticks(x + bar_width / 2)
    ax.set_xticklabels(['Gruppe 1', 'Gruppe 2', 'Gruppe 3'])

    # Titel und Legende anzeigen
    plt.title('2D Balkendiagramm mit Gruppen')
    plt.legend()

    # Diagramm anzeigen
    plt.show()

create_grouped_bar_chart()

目前每组内的两根柱子紧密相邻,视觉效果略显怪异,请问是否可以在每组内的两根柱子之间添加间距?


解决方案

可以通过调整柱子宽度并额外设置组内间距来实现,核心是修改柱子的x坐标偏移量和宽度:

  1. 适当减小单根柱子的宽度,留出间距空间
  2. 定义组内柱子的间距值
  3. 调整第二组柱子的x坐标,在原有的宽度基础上加上间距
  4. 同步调整x轴刻度的位置,确保刻度位于每组柱子的中间

修改后的完整代码:

import matplotlib.pyplot as plt
import numpy as np
def create_grouped_bar_chart():
    # Daten für die Balken
    groups = 3
    bar_width = 0.3  # 减小柱子宽度,留出间距空间
    intra_group_gap = 0.1  # 组内柱子间的间距
    x = np.arange(groups)
    values1 = [2, 3, 4]
    values2 = [4, 5, 2]

    # Balkendiagramm erstellen
    fig, ax = plt.subplots()
    
    # 第一组柱子位置保持不变
    bar1 = ax.bar(x, values1, bar_width, label='Beschriftung 1', hatch='///', edgecolor='black', color='white')
    # 第二组柱子在第一组基础上,加上宽度和组内间距
    bar2 = ax.bar(x + bar_width + intra_group_gap, values2, bar_width, label='Beschriftung 2', edgecolor='black', color='gray')

    # Achsenbeschriftungen
    plt.xlabel('X-Achse')
    plt.ylabel('Y-Achse')
    
    # Achsenbereiche festlegen
    plt.ylim(0, 6)
    plt.yticks(np.arange(0, 7, 1))

    # 调整X轴刻度位置,确保对齐每组柱子的中心
    ax.set_xticks(x + (bar_width + intra_group_gap)/2)
    ax.set_xticklabels(['Gruppe 1', 'Gruppe 2', 'Gruppe 3'])

    # Titel und Legende anzeigen
    plt.title('2D Balkendiagramm mit Gruppen (mit innerem Abstand)')
    plt.legend()

    # Diagramm anzeigen
    plt.show()

create_grouped_bar_chart()

关键修改说明

  • bar_width从0.35改为0.3,缩小单柱宽度避免组间空间拥挤
  • intra_group_gap = 0.1明确设置组内两根柱子的间距大小
  • 第二组柱子的x坐标改为x + bar_width + intra_group_gap,在两组柱子之间插入指定间距
  • ax.set_xticks的位置调整为x + (bar_width + intra_group_gap)/2,保证刻度线精准对齐每组的中心位置

内容的提问来源于stack exchange,提问作者a.urbanek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 21:13:20