我得到了一些关于如何保持条形宽度不变的帮助,无论这里绘制了多少条:How to fix bar width in this matplotlib plot。当我根据绘制的条数来改变条宽时(如最右边的图中),红色和蓝色条不再保持在一起。如何修改此代码,使红色和蓝色条在最后一个图中一起?
import numpy as np
import matplotlib.pyplot as plt
# make some dummy data
scores_reading = np.random.randint(50,100,10)
scores_math = np.random.randint(50,100,10)
fig = plt.figure(figsize=(16,5))
bar_width = 0.35
index = np.arange(10)
ax1 = fig.add_subplot(1,4,1)
ax1.bar(index, scores_reading, bar_width, fc='b', edgecolor='none')
ax1.bar(index+bar_width, scores_math, bar_width, fc='r', edgecolor='none')
ax1.set_xlim(0,10)
ax1.set_title('Original - 10 scores')
ax2 = fig.add_subplot(1,4,2)
ax2.bar(index[:5], scores_reading[:5], bar_width, fc='b', edgecolor='none')
ax2.bar(index[:5]+bar_width, scores_math[:5], bar_width, fc='r', edgecolor='none')
ax2.set_title('Original - 5 scores')
ax3 = fig.add_subplot(1,4,3)
ax3.bar(index[:5], scores_reading[:5], bar_width, fc='b', edgecolor='none')
ax3.bar(index[:5]+bar_width, scores_math[:5], bar_width, fc='r', edgecolor='none')
ax3.set_xlim(0,10)
ax3.set_title('Changed limit - 5 scores')
ax4 = fig.add_subplot(1,4,4)
ax4.bar(index[:5], scores_reading[:5], bar_width/2., fc='b', edgecolor='none')
ax4.bar(index[:5]+bar_width, scores_math[:5], bar_width/2., fc='r', edgecolor='none')
ax4.set_title('Changed width - 5 scores')
fig.show()