On Tue, Feb 15, 2011 at 11:07 PM, Victor Hooi <victorh...@yahoo.com> 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
------------------------------------------------------------------------------
Index, Search & Analyze Logs and other IT data in Real-Time with Splunk 
Collect, index and harness all the fast moving IT data generated by your 
applications, servers and devices whether physical, virtual or in the cloud.
Deliver compliance at lower cost and gain new business insights. 
Free Software Download: http://p.sf.net/sfu/splunk-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to