首页 > 解决方案 > 控制条形图中分组条形之间的空间

问题描述

我想要一个带有 matplotlib 的分组条形图,我使用从https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html获得的代码。不幸的是,酒吧之间的空间太小了,人物的高度也太小了。我查看了不同的网站(例如https://python-graph-gallery.com/5-control-width-and-space-in-barplots/),但无法调整空格。有人可以帮忙吗?

这是我当前情节的屏幕截图:

在此处输入图像描述

这是 Juypter 代码:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


labels = ['Control \n Method 1', 'Method without\n coordination 1', 'Control \n Method 2', 'Control \n Method 3', 'Control \n Method 4' , 'Control \n Method 5']
ed_means = [20, 34, 30, 35, 27, 3]
srdp_means = [25, 32, 34, 20, 25, 3]

x = np.arange(len(labels))  # the label locations
width = 0.30  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, ed_means, width, label='ED')
rects2 = ax.bar(x + width/2, srdp_means, width, label='SRDP')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Optimality in %')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.tick_params(axis='both', which='major', labelsize=12)
ax.legend()



def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
    height = rect.get_height()
    ax.annotate('{}'.format(height),
                xy=(rect.get_x() + rect.get_width() / 2, height),
                xytext=(0, 3),  # 3 points vertical offset
                textcoords="offset points",
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.show()

标签: pythonmatplotlibbar-chart

解决方案


推荐阅读