## matplotlib-users

 [Matplotlib-users] Histogram with single series - colour coding different ranges? From: Victor Hooi - 2011-02-16 05:07:48 Attachments: Message as HTML ```heya, Is there an easy way to colour-code a Matplotlib histogram with a single set of data? So for example, you'd have a bell-shaped histogram, and the middle 50% might be green, the regions 20% to the left and right of that might be yellow, and the 5% either side beyond that could be red. I couldn't seem to find anything in the Matplotlib options for this - any suggestions? Cheers, Victor ```
 Re: [Matplotlib-users] Histogram with single series - colour coding different ranges? From: Benjamin Root - 2011-02-22 16:13:22 Attachments: Message as HTML ```On Tue, Feb 15, 2011 at 11:07 PM, Victor Hooi wrote: > heya, > > Is there an easy way to colour-code a Matplotlib histogram with a single > set of data? > > So for example, you'd have a bell-shaped histogram, and the middle 50% > might be green, the regions 20% to the left and right of that might be > yellow, and the 5% either side beyond that could be red. > > I couldn't seem to find anything in the Matplotlib options for this - any > suggestions? > > Cheers, > Victor > > Sure, check out the following: import matplotlib.pyplot as plt import numpy as np xs = np.arange(20) ys = np.random.rand(20) cs = (['y'] * round(0.25 * len(xs))) + (['g'] * round(0.5 * len(xs))) + (['y'] * round(0.25 * len(xs))) plt.bar(xs, ys, color=cs) plt.show() Admittedly, this isn't using matplotlib's hist() function because it only allows for one color per dataset. However, you can use numpy's histogram function to get the bins and counts yourself, and then use bar() to make the bars. bar() will allow you to color the bars individually. I hope this helps! Ben Root ```
 Re: [Matplotlib-users] Histogram with single series - colour coding different ranges? From: Victor Hooi - 2011-06-14 08:55:56 Attachments: Message as HTML ```Ben, Awesome - thanks for the sample code. I had to make a slight change - multiplying 'y' by a float doesn't work: TypeError: can't multiply sequence by non-int of type 'float' I just did a cast to int, and it worked - not sure if this is a bad practice in Python though?: cs = (['y'] * int(round(0.25 * len(xs)))) + (['g'] * int(round(0.5 * > len(xs)))) + (['y'] * int(round(0.25 * len(xs)))) Anyhow, it's a pity I can't use your code with Matplotlib's hist() - as that definitely made producing histograms bins much easier. It's strange that colour-coding bars isn't a feature of hist(). I guess I'll have to look at doing all the hist setup/calculations by hand. Ah well. Thanks, Victor On Wed, Feb 23, 2011 at 03:12, Benjamin Root wrote: > > > On Tue, Feb 15, 2011 at 11:07 PM, Victor Hooi wrote: > >> heya, >> >> Is there an easy way to colour-code a Matplotlib histogram with a single >> set of data? >> >> So for example, you'd have a bell-shaped histogram, and the middle 50% >> might be green, the regions 20% to the left and right of that might be >> yellow, and the 5% either side beyond that could be red. >> >> I couldn't seem to find anything in the Matplotlib options for this - any >> suggestions? >> >> Cheers, >> Victor >> >> > Sure, check out the following: > > import matplotlib.pyplot as plt > import numpy as np > > xs = np.arange(20) > ys = np.random.rand(20) > cs = (['y'] * round(0.25 * len(xs))) + (['g'] * round(0.5 * len(xs))) + > (['y'] * round(0.25 * len(xs))) > > plt.bar(xs, ys, color=cs) > plt.show() > > > Admittedly, this isn't using matplotlib's hist() function because it only > allows for one color per dataset. However, you can use numpy's histogram > function to get the bins and counts yourself, and then use bar() to make the > bars. bar() will allow you to color the bars individually. > > I hope this helps! > Ben Root > > ```
 Re: [Matplotlib-users] Histogram with single series - colour coding different ranges? From: Goyo - 2011-02-24 23:02:15 ```2011/2/22 Benjamin Root : > > Admittedly, this isn't using matplotlib's hist() function because it only > allows for one color per dataset.  However, you can use numpy's histogram > function to get the bins and counts yourself, and then use bar() to make the > bars.  bar() will allow you to color the bars individually. Pylab hist() returns a list of patches so you can also change their properties. Goyo ```
 Re: [Matplotlib-users] Histogram with single series - colour coding different ranges? From: Victor Hooi - 2011-06-14 09:08:36 Attachments: Message as HTML ```Goyo, Aha, so I can call .set_facecolor() on the Patch objects, and then force the graph to re-draw(). I've tested it - it works when I do savefig(). And if I'm running it interactive, I just run matplotlib.draw() after modifying the patch list. I suppose I can combine that technique with the calculation tip from Ben to colour-code the different region. I could run a for-loop over the Patch list, and do multiple if's - however, wondering if there's a way to do it with a list-comprehension? Thanks for the tips, guys =). Cheers, Victor On Fri, Feb 25, 2011 at 10:02, Goyo wrote: > 2011/2/22 Benjamin Root : > > > > Admittedly, this isn't using matplotlib's hist() function because it only > > allows for one color per dataset. However, you can use numpy's histogram > > function to get the bins and counts yourself, and then use bar() to make > the > > bars. bar() will allow you to color the bars individually. > > Pylab hist() returns a list of patches so you can also change their > properties. > > Goyo > ```