[Solved] Interactively Re-color Bars in Matplotlib Bar Chart using Confidence Intervals


This is how I would handle this:

def recolorBars(self, event):      
    y = event.ydata
    for i, rect in enumerate(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

solved Interactively Re-color Bars in Matplotlib Bar Chart using Confidence Intervals