how to change the order of factor plot in seaborn

MYjx picture MYjx · Jul 7, 2014 · Viewed 19.2k times · Source

My data looks like this:

m=pd.DataFrame({'model':['1','1','2','2','13','13'],'rate':randn(6)},index=['0', '0','1','1','2','2'])

I want to have the x-axis of factor plot ordered in [1,2,13] but the default is [1,13,2].

Does anyone know how to change it?

Update: I think I have figured it out in the following way, but maybe there is a better way by using an index to do that?

sns.factorplot('model','rate',data=m,kind="bar",x_order=['1','2','13'])

Answer

mwaskom picture mwaskom · Jul 7, 2014

Your update to the post shows the correct way to do it, i.e. you should pass a list of x values to order in the order you want them plotted. The default for numeric data is to plot in sorted order, so if you have numeric values it's best to keep them as integers or floats instead of strings, so they will be in "natural" order.