Search code examples
python-3.xpandasdataframesortingboxplot

Is there a way to prevent the auto sorting when using the dataframe.boxplot() function?


I need to label the columns and have them displayed in the original sequence.

Here is what I did :

df['Column'] = df['Column'].map({1: 'L5', 2:'L4', 3: 'L3', 4:'L2', 5:'L1', 6:'M12', 7:'M11', 8:'M10', 9:'M9', 10:'M8', 11:'M7', 12:'M6', 13:'M5', 14:'M4'})

sorted()

df.boxplot(column=['Time'], by=['Column'])

However, the plot appears to be in no particular order

1


Solution

  • The plot appears in lexical order of the new column value. If you want the original order, you can force it with pd.Categorical:

    d = {1: 'L5', 2:'L4', 3: 'L3', 4:'L2', 5:'L1', 6:'M12', 7:'M11', 8:'M10', 9:'M9', 10:'M8', 11:'M7', 12:'M6', 13:'M5', 14:'M4'}
    
    df['Column'] = pd.Categorical(df['Column'].map(d), ordered=True, categories=list(d.values()))
    
    df.boxplot(column=['Time'], by=['Column'])
    

    Or you can just relabel the ticks:

    # no map
    # df['Column'] = df['Column'].map(d)
    
    # grab the axis returned by .boxplot
    ax = df.boxplot(column=['Time'], by=['Column'])
    
    # rename the tick labels, `d` as above
    ax.set_xticklabels(list(d.values()))
    

    Either of them gives you:

    enter image description here