matplotlib 中有两个变量的直方图

Histogram with two variable in matplotlib

我有这样的数据集:

ID  z  N
0   0.15    69.0
1   0.25    208.0
2   0.35    402.0
3   0.45    223.0
4   0.55    327.0
5   0.65    136.0
6   0.75    136.0
7   0.85    136.0
8   0.95    136.0
9   1.05    136.0
10  1.15    136.0
11  1.25    136.0
12  1.35    136.0
13  1.45    136.0
14  1.55    136.0
15  1.65    136.0

我想做一个这样的情节

我找不到出路。一个简单的 plt.hist() 是一个单函数图。或者 plt.bar(z,N) 不会消除柱之间的线条。

那是因为 plt.hist 需要一个值列表,它将从中计算频率。由于您已经有了频率,因此您可以重新制作值列表并让 plt.hist

的方式工作
import matplotlib.pyplot as plt

z = [0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95, 1.05, 1.15, 1.25, 1.35, 1.45, 1.55, 1.65]
N = [69.0, 208.0, 402.0, 223.0, 327.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0]
hist_vals = []
for n,zz in zip(N,z):
    hist_vals += [zz]*int(n)
plt.hist(hist_vals,bins=z+[1.7], histtype='step', edgecolor='k')
plt.show()