Seaborn PointPlot 类别排序问题 [英] Seaborn PointPlot Category Ordering Issue

查看:40
本文介绍了Seaborn PointPlot 类别排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 Seaborn 中使用 pointplot,总的来说它可以满足我的需求,而且我对它的功能印象深刻.

I have been trying to use pointplot in Seaborn and in general it does what I need and I'm very impressed with the functionality.

我的用例相当标准:

sns.pointplot(x="Category_1", y="Parameter", hue="Category 2", data=df);

我的问题是我用于色调的类别包含以下形式的字符串:

My issue is that the Category I use for the hue contains strings in the form:

"1 string1", "2 string2", "3 string3", etc....

这是设计使然,这样我就可以在排序时获得特定的顺序.但是,当我运行 Seaborn 函数时,顺序是不同的.

This is by design so that I can get a particular order if I sort. However when I run the Seaborn function the order is different.

Seaborn 是按照什么逻辑来排序的,有没有办法强制执行所需的顺序.如果我这样做:

By what logic does Seaborn order this, and is there a way for forcing a desired order. If I do:

A = df.Category2.unique()
A.sort()

我得到了我想要的列表顺序.

I get the list order that I want.

推荐答案

教程中所述,分类绘图函数遵循一组规则来确定绘图元素的顺序:

As explained in the tutorial, categorical plotting functions follow a set of rules to determine the order of plot elements:

  • 如果将类别序列传递给 order 参数,则使用该顺序.
  • 如果变量是具有指定类别顺序的熊猫Categorical,则使用该顺序.
  • 如果变量是数字,则值按排序顺序显示
  • 否则,这些值将按照它们在 DataFrame 中出现的顺序显示.
  • If a sequence of categories is passed to the order parameter, that order is used.
  • If the variable is a pandas Categorical with an assigned category order, that order is used.
  • If the variable is numeric, the values are shown in sorted order
  • Otherwise, the values are shown in the order that they appear in the DataFrame.

这篇关于Seaborn PointPlot 类别排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆