Python/matplotlib Show confidence levels in a histogram
here is my problem. I have some data I binned to obtain a "digitalised" pdf, and that's fine. Now, I wanted to find a way to indicate different confidence intervals by coloring the bin groups differently. In particular, startin开发者_开发知识库g with the bin containing the highest count I wanted to find and colour, say red, all the highest bins whose area sum to less than say .6. Then, always picking new bins by decreasing counts, i want to colour the bins who increase my red area to .8 in orange. I was considering using numpy to get bins and counts, sort them into 3 series (red, orange and original colour) and plot them with pyplot's bar. Hope you can point out a faster way, thanks!
If I understand your question correctly, I think the code below will do what you are suggesting. It seems a little different than the approach you were considering, and I'm not sure it is more efficient. Regardless, it usually helps to see the way someone else would do something. It assumes an already generated pdf (histogram) with a bins represented by the varialble "bins" and bin width represented by the variable "binwidth".
gray = (.5,.5,.5)
orange = (1.0, 0.647, 0.0)
red = (1.0, 0.0, 0.0)
clrs = [gray for xx in bins]
idxs = pdf.argsort()
idxs = idxs[::-1]
oranges = idxs[(cumsum(pdf[idxs])*binwidth < 0.8).nonzero()]
reds = idxs[(cumsum(pdf[idxs])*binwidth < 0.6).nonzero()]
for idx in oranges:
clrs[idx] = orange
for idx in reds:
clrs[idx] = red
bar(left=bins,height=pdf,width=binwidth,color=clrs)
精彩评论