栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

index = np.arange(n_groups)

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

import numpy as npimport matplotlib.pyplot as plt# make some dummy datascores_reading = np.random.randint(50,100,10)scores_math = np.random.randint(50,100,10)fig = plt.figure(figsize=(16,5))bar_width = 0.35index = 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()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号