Boxplot stratified by column in python pandas

I would like to draw a boxplot for the following pandas dataframe:

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

I want the boxes to be column "N0_YLDF", but they should be stratified by "MAT". When I use foll. Team:

 p1.boxplot(column='N0_YLDF',by='MAT') 

It uses all unique MAT values, which in the total number of p1 frame data are about 15,000. This leads to an incomprehensible box.

Is there any way to calculate the MAT values ​​to get the first square N0_YLDF for the first quartile of MAT values, etc ...

thanks!

+7
python matplotlib pandas boxplot
source share
2 answers

pandas.qcut will provide you with quantiles, but for the operation with the histogram, you will need some numpy blende, which is useful 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]) 

enter image description here

Now the data frame is as follows:

  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 obtain quartiles:

 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]) 

enter image description here

+5
source share

Pandas has cut and qcut functions to make stratification variables such as this easy:

 # Just asking for split into 4 equal groups (ie quartiles) here, # but you can split on custom quantiles by passing in an array p1['MAT_quartiles'] = pd.qcut(p1['MAT'], 4, labels=['0-25%', '25-50%', '50-75%', '75-100%']) p1.boxplot(column='N0_YLDF', by='MAT_quartiles') 

Output:

enter image description here

+8
source share

All Articles