Histogram with two variable in matplotlib

Viewed 441

I have a data set like this :

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

I would like to make a plot this enter image description here

I cannot find the way out. A simple plt.hist() is a single function plot. Or a plt.bar(z,N) wont dissolve the lines between bars.

1 Answers

That's because plt.hist is expecting a list of values from which it will calculate the frequencies. Since you already have the frequencies, you could remake the list of values and let plt.hist work the way it was made to

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()

Histogram

Related