I have the following problem. I have some data for which I need to make an histogram.
For this purpose I use the numpy.histogram
to generate the bins and the content and then pyplot.stairs
for the visualization.
So far so good.
Next part is that I would like to have the area of the histogram between the left end and a given value x filled in a color, then from x to y in another and finally from y to the right end in a third one.
Something like the image here below.
Do you know how to do it?
Thanks!
I can see two options here.
One, close to your code (well, you didn't really provide any code, so I had to surmise a bit)
# MRE part
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as sta
data=sta.skewnorm(-0.6,0.25,0.25).rvs(70000)
# Your (probable) code
eff,edges=np.histogram(data, bins=200)
plt.stairs(eff, edges)
plt.show()
# Multi-color version
plt.stairs(eff[:85], edges[:86], fill=True, color='red')
plt.stairs(eff[85:110], edges[85:111], fill=True, color='green')
plt.stairs(eff[110:], edges[110:], fill=True, color='blue')
plt.show()
Another method would be to rely on hist
method of matplotlib. And colors the bars as you wish. For example
eff, edges, bars = plt.hist(data, bins=200, color='green')
centres = (edges[1:]+edges[:-1])/2
for i in np.where(centres<0)[0]: bars[i].set_facecolor('red')
for i in np.where(centres>0.25)[0]: bars[i].set_facecolor('blue')
In your image you seems to have an outline drawn. Of course, you can always do that by drawing a plt.stairs(fill=False)
in addition to any of those solutions.
Same result of course.