Python 如何在此matplotlib绘图中固定条形图宽度

Python 如何在此matplotlib绘图中固定条形图宽度,python,python-2.7,matplotlib,Python,Python 2.7,Matplotlib,有人能告诉我如何修改这个使用matplotlib的Python代码,以便无论绘制多少分数,条的宽度都保持不变吗?提前谢谢你 # data to plot n_groups = len(math_scores) ###print "im here", math_scores,verbal_scores scores_readingwriting = verbal_scores scores_math = math_scores # create plot fig, ax =

有人能告诉我如何修改这个使用matplotlib的Python代码,以便无论绘制多少分数,条的宽度都保持不变吗?提前谢谢你

# data to plot
  n_groups = len(math_scores)
  ###print "im here", math_scores,verbal_scores
  scores_readingwriting = verbal_scores
  scores_math = math_scores

# create plot
  fig, ax = plot.subplots()
  index = np.arange(n_groups)
  bar_width = 0.35
  opacity = 0.8

  rects1 = plot.bar(index, scores_readingwriting, bar_width,
                 alpha=opacity,
                 color='black',
                 label='R/W')

  rects2 = plot.bar(index + bar_width, scores_math, bar_width,
                 alpha=opacity,
                 color='grey',
                 label='Math')

  plot.xlabel('Date',size='14')
  plot.ylabel('Scores',size='14')

  plot.title(str(first_names[i])+' '+str(last_names[i])+"'s History",size='17')
  num=len(scores)
  ###print datesofinterest
  plot.xticks(index + bar_width/2, datesofinterest,size='12')
  plot.yticks(size='12')
  axes = plot.gca()
  axes.set_ylim([200,800])
  plot.legend()

  plot.tight_layout()
  fig.savefig('img'+str(student_ids[i])+'.png')

这些条的宽度是一样的!它们看起来不一样的原因是因为您使用的是这一行:

index = np.arange(n_groups)
这将更改x值,从而更改x轴的比例。为了抵消这种影响,您可以更改x轴的限制,或者使条形宽度取决于分数的数量,因此,例如,如果宽度为0.35适用于10个分数,则如果分数的数量增加一倍,则将使条形宽度减半(反之亦然)。您可以看到更改轴限制或条形宽度如何使条形看起来具有相同的宽度:

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()

谢谢!我检查了一下这是否有效。我唯一的后续问题是,如何使第二个条形图中5个分数的条形图更接近?钢筋应始终保持接触。再次感谢您的帮助。:)你犯的错误是在红条上。您将
bar\u width
添加到
索引[:5]
中,但蓝色条的大小为
bar\u width/2
。所以这一行应该像这个ax4.bar(索引[:5]+(bar\u宽度/2),数学分数[:5],bar\u宽度/2.,fc='r',edgecolor='none')谢谢@FemiOladeji-这将移动这些条,使它们保持接触:-)