如何在这个matplotlibp p中固定条宽度

2024-10-01 11:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我如何修改这个使用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')

Tags: sizeindexlenplotfigbarmathwidth
1条回答
网友
1楼 · 发布于 2024-10-01 11:28:03

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

index = np.arange(n_groups)

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

^{pr2}$

enter image description here

相关问题 更多 >