【问题标题】:How to fix bar width in this matplotlib plot如何在这个 matplotlib 图中修复条形宽度
【发布时间】:2017-11-04 13:20:57
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

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

    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_width 添加到index[:5],但蓝色条的大小为​​bar_width/2。所以这条线应该是这样 ax4.bar(index[:5]+(bar_width/2), scores_math[:5], bar_width/2., fc='r', edgecolor='none')
    • 谢谢@FemiOladeji - 这会移动酒吧,让他们保持接触:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2019-09-14
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多