python - How to apply custom column order to pandas boxplot? -


i can boxplot of salary column in pandas dataframe...

train.boxplot(column='predictionerror',by='category',sym='') 

...however can't figure out how define index-order used on column 'category' - want supply own custom order, according criterion:

category_order_by_mean_salary = train.groupby('category')['salary'].mean().order().keys() 

how can apply custom column order boxplot columns? (other ugly kludging column names prefix force ordering)

'category' string column taking 27 distinct values: ['accounting & finance jobs','admin jobs',...,'travel jobs']. can factorized pd.categorical.from_array()

on inspection, limitation inside pandas.tools.plotting.py:boxplot(), converts column object without allowing ordering:

i suppose either hack custom version of pandas boxplot(), or reach internals of object. , file enhance request.

edit: question arose pandas ~0.13 , has been obsoleted recent (0.19+?) versions per @cireo's late answer.

hard how without working example. first guess add integer column orders want.

a simple, brute-force way add each boxplot 1 @ time.

import numpy np import pandas pd import matplotlib.pyplot plt  df = pd.dataframe(np.random.rand(37,4), columns=list('abcd')) columns_my_order = ['c', 'a', 'd', 'b'] fig, ax = plt.subplots() position, column in enumerate(columns_my_order):     ax.boxplot(df[column], positions=[position])  ax.set_xticks(range(position+1)) ax.set_xticklabels(columns_my_order) ax.set_xlim(xmin=-0.5) plt.show() 

enter image description here


Comments