如何在此matplotlib图中固定条形宽度 [英] How to fix bar width in this matplotlib plot

查看:76
本文介绍了如何在此matplotlib图中固定条形宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何修改这个使用 matplotlib 的 Python 代码,这样无论绘制了多少分数,条形的宽度都将保持不变吗?预先谢谢你!

Could someone please tell me how to modify this Python code that uses matplotlib such that the width of the bars will stay constant regardless of how many scores are plotted? Thank you in advance!

# 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')

推荐答案

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

The bars are the same width! The reason they don't look the same is because you are using this line:

index = np.arange(n_groups)

这将更改您的 x 值,从而更改 x 轴的比例.要抵消这种效果,您可以更改x轴的限制,也可以使条形宽度取决于分数的数量,例如,如果0.35的宽度适用于10个分数,那么如果将分数的数量加倍,您会条宽的一半(反之亦然).您可以看到更改轴限制或条形宽度如何使条形看起来具有相同的宽度:

This will change your x-values and hence the scale of your x-axis. To counteract this effect you can either change the limits of your x-axis, or make the bar width depend on the number of scores, so for example if a width of 0.35 works for 10 scores then if you double the number of scores you would half the bar width (and vice versa). You can see how either changing the axis limits or bar width gives bars that look the same width:

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

这篇关于如何在此matplotlib图中固定条形宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆