I've the following code that I'm using to plot a numpy array as a histogram. All I end up getting is a box.
from sys import argv as a
import numpy as np
import matplotlib.pyplot as plt
r = list(map(int, (a[1], a[2], a[3], a[4], a[5])))
s = np.array([int((x - min(r))/(max(r) - min(r)) * 10) for x in r])
plt.hist(s, normed=True, bins=5)
plt.show()
The program is run with the following inputs 22 43 11 34 26
How can I get a histogram with the y axis representing the heights of list elements.
The plt() function present in pyplot submodule of Matplotlib takes the array of dataset and array of bin as parameter and creates a histogram of the corresponding data values.
plt() Matplotlib can convert this numeric representation of histogram into a graph. The plt() function of pyplot submodule takes the array containing the data and bin array as parameters and converts into a histogram.
The Numpy histogram function doesn't draw the histogram, but it computes the occurrences of input data that fall within each bin, which in turns determines the area (not necessarily the height if the bins aren't of equal width) of each bar. There are 3 bins, for values ranging from 0 to 1 (excl 1.), 1 to 2 (excl.
You can use plt.bar
:
plt.bar(np.arange(len(s)),s)
plt.show()
Which turns into the below. Is that your expected output?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With