Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a numpy array as a histogram

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.

like image 454
Melissa Stewart Avatar asked Apr 06 '17 20:04

Melissa Stewart


People also ask

How do you plot a histogram of a NumPy array in Python?

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.

How do you plot an array of a histogram?

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.

How does NumPy histogram work?

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.


1 Answers

You can use plt.bar:

plt.bar(np.arange(len(s)),s)
plt.show()

Which turns into the below. Is that your expected output?

enter image description here

like image 67
mechanical_meat Avatar answered Sep 28 '22 00:09

mechanical_meat