how to change the order of factor plot in seaborn

雨燕双飞 提交于 2020-01-11 08:11:30

问题


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

回答1:


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.




回答2:


From the documentation it appears that the seaborn API has updated again, the argument x_order should be replaced by order:

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

Also, factorplot has been renamed and will be removed in future releases; it is replaced by catplot:

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


来源:https://stackoverflow.com/questions/24618862/how-to-change-the-order-of-factor-plot-in-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!