Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a histogram given bin endpoints and values

Say I have an array of bin edges, and an array of bin values. (basically the output of plt.hist). For instance:

bins = np.array([1, 2, 3, 4, 5])
vals = np.array([2, 5, 5, 2])

How do I plot this as a histogram?

Edit: for clarity, I mean vals to be the "height" of each bin, where len(vals) + 1 = len(bins)

like image 950
Peter Avatar asked Jan 21 '26 03:01

Peter


1 Answers

If you are using python 3.5 you can use pyplot fill_between function for such. You can use following code:

import numpy as np
import matplotlib.pyplot as plt
bins = np.array([1, 2, 3, 4, 5])
vals = np.array([2, 5, 5, 2])

plt.fill_between(bins,np.concatenate(([0],vals)), step="pre")
plt.show()

This will generate below graph: graph with step command

like image 177
Cedric Zoppolo Avatar answered Jan 23 '26 04:01

Cedric Zoppolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!