A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/23232989/boxplot-stratified-by-column-in-python-pandas below:

matplotlib - Boxplot stratified by column in python pandas

pandas.qcut will give you the quantiles, but a histogram-like operation will require some numpy trickery which comes in handy here:

_, breaks = np.histogram(df.MAT, bins=5)
ax = df.boxplot(column='N0_YLDF', by='Class')
ax.xaxis.set_ticklabels(['%s'%val for i, val in enumerate(breaks) if i in df.Class])

The dataframe now looks like this:

   N0_YLDF    MAT  Class
0     1.29  13.67      1
1     2.32  10.67      0
2     6.24  11.29      1
3     5.34  21.29      1
4     6.35  41.67      2
5     5.35  91.67      5
6     9.32  21.52      1
7     6.32  31.52      2
8     3.33  13.52      1
9     4.56  44.52      3

[10 rows x 3 columns]

It can also be used to get the quartile plot:

breaks = np.asarray(np.percentile(df.MAT, [25,50,75,100]))
df['Class'] = (df.MAT.values > breaks[..., np.newaxis]).sum(0)
ax = df.boxplot(column='N0_YLDF', by='Class')
ax.xaxis.set_ticklabels(['%s'%val for val in breaks])


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4