Python / matplotlib Show confidence levels in a histogram

here is my problem. I have some data that I received to get a digital pdf version, and that’s fine. Now I wanted to find a way to specify different confidence intervals by coloring groups of beans in different ways. In particular, starting with the bunker containing the highest score, I wanted to find and color, say, red, all the highest bunkers, the area of ​​which is less than they say .6. Then, always choosing new bins, decreasing the amount, I want to color the bins that increase my red area to 0.8 in orange. I was thinking about using numpy to get bins and counts, sort them into 3 series (red, orange and original color) and draw them using the pyplot panel. Hope you can specify a faster way, thanks!

+3
source share
1 answer

If I understand your question correctly, I think the code below will do what you offer. This is slightly different from the approach you were considering, and I'm not sure if it is more effective. Despite this, it usually helps to see how someone else will do something. It assumes an already created pdf (histogram) with bins represented by varialble "bins" and the bin width represented by the "binwidth" variable.

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)

Here is a view of the result that I get along with the associated PDF and CDF

+6
source

All Articles