Skip to content Skip to sidebar Skip to footer

Interactively Re-color Bars In Matplotlib Bar Chart Using Confidence Intervals

Trying to shade the bars in this chart based on the confidence that a selected y-value (represented by the red line) lies within a confidence interval. See recolorBars() method in

Solution 1:

This is how I would handle this:

defrecolorBars(self, event):      
    y = event.ydata
    for i, rect inenumerate(self.rects):
        t, p, _ = sms.DescrStatsW(self.df[self.df.columns[i]]).ttest_mean(y)
        rect.set_color(self.cpick.to_rgba((1 - p) * t / abs(t)))

When iterating through the bars, first test the value against the sample mean, then set the color based on p-value and test statistic t: (1 - p) * t

Also, you must define cpick at the same time as cmap and set it to (-1, 1) using:

cpick = cm.ScalarMappable(cmap=cmap)
cpick.set_array(np.linspace(-1, 1))

The modifications above got me this result

Post a Comment for "Interactively Re-color Bars In Matplotlib Bar Chart Using Confidence Intervals"