Question

I have a DataFrame containing different estimates and p-values for several statistical models as columns.

df = pd.DataFrame({'m4_params':np.random.normal(size=3),
                   'm4_pvalues':np.random.random_sample(3),
                   'm5_params':np.random.normal(size=3),
                   'm5_pvalues':np.random.random_sample(3),
                   'm6_params':np.random.normal(size=3),
                   'm6_pvalues':np.random.random_sample(3)})

I can convert this into the desired barchart by transposing and plotting as a barh:

df[['m4_params','m5_params','m6_params']].T.plot(kind='barh')

However, I'd also like to update these bars charts by changing the alpha channel of each bar based on its corresponding pvalue with a simple function like alpha = 1 - pvalue.

The goal is to make the bars with higher levels of significance stronger while those with weaker significance more transparent. As far as I know, the alpha keyword only accepts floats, which means that I need some way of accessing the properties of each bar in the plot.

Était-ce utile?

La solution

This approach may be brittle (tested with pandas 0.11.0), but you could iterate over the axes.patches list. A more reliable approach would be to build the bar plot yourself with calls to plt.barh(). (side note: very small alphas were invisible, so I set the minimum alpha to .2)

from itertools import product

ax = df[['m4_params','m5_params','m6_params']].T.plot(kind='barh')
for i, (j, k) in enumerate(product(range(3), range(3))):
    patch = ax.patches[i]
    alpha = 1 - df[['m4_pvalues','m5_pvalues','m6_pvalues']].T.iloc[j, k]
    patch.set_alpha(max(alpha, .2))
plt.draw()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top