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

直方图:Python matplotlib.bar 使用 for 循环

直方图:Python matplotlib.bar 使用 for 循环

扬帆大鱼 2021-12-08 16:31:48
试图我试图用matplotlib 3.0.3在python3.7 中绘制一些直方图,但遇到了一个问题:代码:ind = np.arange(2)width = 0.35data = [(0.5, 0.1), (0.8, 0.3)]for i in data:    plt.bar(ind, i[0], width, yerr=i[1])plt.ylabel('scratchwidth /cm')plt.show输出:我希望有两个条形图,(0|0.5)以及(1|0.8)不确定性0.1和0.3。我得到的是两个条形,它们的y=0.8和0.3的不确定性。plt.bar()在 for 循环中不起作用?我该如何解决这个问题?
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

您必须将 Y 数组和 Err 数组传递给函数bar。将数据从点数组转换为 Y 和 Err 数组:


ind = np.arange(2)

width = 0.35

data = [(0.5, 0.1), (0.8, 0.3)]

y, err = list(zip(*data)) # Transpose the data array

plt.bar(ind, y, width, yerr=err)

plt.ylabel('scratchwidth /cm')

plt.show()

或者,由于您已经使用 numpy,请使用 numpy 数组:


....

data = np.array([(0.5, 0.1), (0.8, 0.3)])

plt.bar(ind, data[:,0], width, yerr=data[:,1])

....


查看完整回答
反对 回复 2021-12-08
  • 1 回答
  • 0 关注
  • 336 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信