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

直方图:Python matplotlib.bar使用for循环 企图,python,matplotlib,histogram,Python,Matplotlib,Histogram,我试图用matplotlib 3.0.3在python3.7中绘制一些直方图,但遇到了一个问题: 代码: 产出: 我期望一个有两条线的图,(0.5)和(1.8),不确定性0.1和0.3。我得到的是两个条,它们都具有y=0.8和0.3的不确定性。plt.bar()在for循环中不工作吗? 我如何解决这个问题 必须将Y和Err数组传递给函数bar。将数据从点阵列转换为Y和Err阵列: ind = np.arange(2) width = 0.35 data = [(0.5, 0.1), (0.8,

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

代码: 产出: 我期望一个有两条线的图,
(0.5)
(1.8)
,不确定性
0.1
0.3
。我得到的是两个条,它们都具有y=0.80.3的不确定性。
plt.bar()
在for循环中不工作吗?
我如何解决这个问题

必须将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])
....

您必须将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])
....