I am trying to plot a histogram using the matplotlib.hist()
function but I am not sure how to do it.
I have a list
probability = [0.3602150537634409, 0.42028985507246375,
0.373117033603708, 0.36813186813186816, 0.32517482517482516,
0.4175257731958763, 0.41025641025641024, 0.39408866995073893,
0.4143222506393862, 0.34, 0.391025641025641, 0.3130841121495327,
0.35398230088495575]
and a list of names(strings).
How do I make the probability as my y-value of each bar and names as x-values?
This is a very round-about way of doing it but if you want to make a histogram where you already know the bin values but dont have the source data, you can use the
np.random.randint
function to generate the correct number of values within the range of each bin for the hist function to graph, for example:as for labels you can align x ticks with bins to get something like this:
If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins:
However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis:
Let me know if this solves your problem.
EDIT 26 November 2018
As per comment below, the following code will suffice as of Matplotlib 3.0.2:
EDIT 23 May 2019
As far as histogram is concerned, the
normed
param is deprecated:So, as from
Matplolib 3.1
instead of:one has to write:
If you haven't installed matplotlib yet just try the command.
Library import
The histogram data:
Display histogram
And the output is like :