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

试图

我试图用matplotlib 3.0.3在python3.7 中绘制一些直方图,但遇到了一个问题:


代码:

ind = np.arange(2)

width = 0.35

data = [(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 循环中不起作用?我该如何解决这个问题?


扬帆大鱼
浏览 337回答 1
1回答

动漫人物

您必须将 Y 数组和 Err 数组传递给函数bar。将数据从点数组转换为 Y 和 Err 数组:ind = np.arange(2)width = 0.35data = [(0.5, 0.1), (0.8, 0.3)]y, err = list(zip(*data)) # Transpose the data arrayplt.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])....
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python