pandas 如何更改seaborn中因子图的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24618862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change the order of factor plot in seaborn
提问by MYjx
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].
我想在 [1,2,13] 中排序因子图的 x 轴,但默认值为 [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'])
回答by mwaskom
Your update to the post shows the correct way to do it, i.e. you should pass a list of xvalues to orderin 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.
您对帖子的更新显示了正确的方法,即您应该按照您希望绘制的顺序传递x值列表order。数值数据的默认值是按排序顺序绘制,因此如果您有数值,最好将它们保留为整数或浮点数而不是字符串,这样它们将按“自然”顺序排列。
回答by Archie
From the documentationit appears that the seaborn API has updated again, the argument x_ordershould be replaced by order:
从文档看来,seaborn API 已再次更新,该参数x_order应替换为order:
sns.factorplot('model', 'rate', data=m, kind="bar", order=['1','2','13'])
Also, factorplothas been renamed and will be removed in future releases; it is replaced by catplot:
此外,factorplot已重命名并将在未来版本中删除;它被替换为catplot:
sns.catplot('model', 'rate', data=m, kind="bar", order=['1','2','13'])

