为了账号安全,请及时绑定邮箱和手机立即绑定

Matplotlib 在某些点添加条之间的距离并调整 xticks

Matplotlib 在某些点添加条之间的距离并调整 xticks

蛊毒传说 2022-05-19 15:45:02
嘿,我在 matplotlib 中有这个条形图,我想在红线所在的位置(不是红条)之间添加一些空白,并调整 x-ticks,使它们位于每组条形之下。对于我现在在 jupyter 笔记本中的内容,我有以下最小示例(尽管使用的是假数据集):import numpy as npimport matplotlib.pyplot as pltset1 = [16.4, 9.5, 9.53, 9.57, 9.0]set2 = [0.8, 1.90, 1.90, 1.90, 1.90]set3 = [16.19, 2.86, 2.86, 2.86, 13.55]set4 = [12721.93, 3.81, 3.81, 3.81, 3.81]x = np.arange(5)plt.bar(x+0.00, set1, color = 'b', hatch="/", width=0.25)plt.bar(x+0.25, set2, color = 'r', hatch="\\", width=0.25)plt.bar(x+0.50, set3, color = 'g', hatch="x", width=0.25)plt.bar(x+0.75, set4, color = 'y', hatch="*", width=0.25)plt.xticks(np.arange(5, step=1), ['Set1', 'Set2', 'Set3', 'Set4', 'Set5'])plt.yscale('log',basey=10)plt.tight_layout()对于如何使用 matplotlib 实际执行此操作的想法,我完全空白,因此不胜感激。
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

x您可以通过更改第一个参数 ( ) 和 的参数width来调整条的放置位置和条的宽度plt.bar。像这样的东西应该工作:


plt.bar(x-0.3, set1, color = 'b', hatch="/", width=0.2)

plt.bar(x-0.1, set2, color = 'r', hatch="\\", width=0.2)

plt.bar(x+0.1, set3, color = 'g', hatch="x", width=0.2)

plt.bar(x+0.3, set4, color = 'y', hatch="*", width=0.2)

//img1.sycdn.imooc.com//6285f70700019d7d04280288.jpg

查看完整回答
反对 回复 2022-05-19
  • 1 回答
  • 0 关注
  • 220 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号