Laura
Laura

Reputation: 1

how I plot three histograms in the same figure where each of them must represent a range of values?

I'm new in this forum. I've searched for a long time my answer in precedent posts, but no answer satisfied my trouble fully. I read post that are in the follow links

Multiple Histograms, each for a label of x-axis, on the same graph matplotlib

Plot two histograms at the same time with matplotlib

and many others, but nothing.

So, I decide to ask you my question. I have two arrays of probabilities like these (for simplicity I report two little lists): a = [0.1, 0.2, 0.4, 0.56, 0.67, 0.70, 0.89, 0.90] b = [0.15, 0.22, 0.41, 0.47, 0.45, 0.59, 0.66, 0.75, 0.83, 0.99]

I must create a histograms that represent three group of bars formed by 2 bars (one for the array a and another for the array b).

The first group of bars must represent the values of arrays that are between 0.0 (included) and 0.4 (excluded), the second group must represent the values of arrays that are between 0.4(included) and 0.65 (excluded), and the last group must represent the remaining values. On the y axis I would prefer have relative frequency (instead of absolute frequency).
I should be obtain something like this https://ibb.co/41BdCCP (that I found in https://plot.ly/python/bar-charts/), but obviously, on the x axis I would the range of values (instead of animals name) and on the y axis I would relative frequency (like I wrote before).

Thank you so much, I hope that someone is able to resolve my problem.

Upvotes: 0

Views: 248

Answers (1)

Sheldore
Sheldore

Reputation: 39072

I don't know what you eactly want: a bar chart or a histogram. But here is a histogram based on your question:

Here I am using a mask to define the groups you want to plot. You can adapt the solution to your problem. I created some test arrays for example purpose

a = np.array([0.04, 0.09,0.1, 0.12, 0.2, 0.4, 0.42, 0.44, 0.47, 0.5, 0.53,0.56, 0.67, 0.70, 0.75, 0.76, 0.78, 0.79, 0.89, 0.90] )
b = np.array([0.05, 0.08, 0.15,0.12, 0.22, 0.41, 0.43, 0.44, 0.46, 0.47, 0.45, 0.51, 0.54,0.59, 0.66, 0.75, 0.75, 0.76, 0.77, 0.8, 0.83, 0.99])

lim = [0, 0.4, 0.65, 1]
for i in range(len(lim)-1):
    plt.hist(a[(a>=lim[i]) & (a<lim[i+1])], color='r')
    plt.hist(b[(b>=lim[i]) & (b<lim[i+1])], color='b')

enter image description here

Upvotes: 0

Related Questions