首页 > 解决方案 > 重叠的matplot条形图?

问题描述

我正在用python学习matplot,我想用并排的条形图制作一个条形图。由于某种原因,我的情节目前正在重叠。

图形

import numpy as np
import matplotlib.pyplot as plt

n_groups = 7
means_frank = (82, 75, 86, 63, 90, 73 ,88)
means_alex = (91, 92, 80, 73, 83, 91, 71)
means_joe = (72, 42, 50, 33, 63, 34, 54)


fig = plt.figure()
ax = fig.add_subplot(111)


index = np.arange(n_groups)
bar_width = 0.27
opacity = 0.8

rects1 = ax.bar(index,means_frank,bar_width,color='b', label="Frank")

rects2 = ax.bar(index,means_alex,bar_width,color='g', label="Alex")

rects3 = ax.bar(index,means_joe,bar_width,color='r', label="Joe")

plt.ylabel('Scores')
plt.title('Test Scores')
plt.xticks([0, 5, 6], ["Assignments -->", "<-- Midterm", "Final"])
plt.legend()

plt.tight_layout()


plt.show()

如何使这 3 个不同的图并排而不是重叠出现?

谢谢!

标签: pythonmatplotlib

解决方案


@ImportanceofBeingErnest 帮助了这个链接: https ://matplotlib.org/gallery/lines_bars_and_markers/barchart.html

必须修改 index 参数以防止重叠。


推荐阅读