pandas Seaborn.relplot() 中的 `hue` 参数在给定数值数据时会跳过一个整数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51525284/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:50:41  来源:igfitidea点击:

The `hue` parameter in Seaborn.relplot() skips an integer when given numerical data?

pythonpandasseaborn

提问by Bstampe

The hue parameter skips one integer.

色调参数跳过一个整数。

d = {'column1':[1,2,3,4,5], 'column2':[2,4,5,2,3], 'cluster':[0,1,2,3,4]}

df = pd.DataFrame(data=d)

sns.relplot(x='column2', y='column1', hue='cluster', data=df)

While all points are plotted, the cluster label is missing '2'.

虽然绘制了所有点,但聚类标签缺少“2”。

Python 2.7 Seaborn 0.9.0 Ubuntu 16.04 LTS

Python 2.7 Seaborn 0.9.0 Ubuntu 16.04 LTS

回答by ImportanceOfBeingErnest

"Full" legend

“全”传奇

If the hueis in numeric format, seaborn will assume that it represents some continuous quantity and will decide to display what it thinks is a representative sample along the color dimension.

如果hue是数字格式,seaborn 会假设它代表某个连续的数量,并决定沿颜色维度显示它认为是代表性的样本。

You can circumvent this by using legend="full".

您可以使用legend="full".

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame({'column1':[1,2,3,4,5], 'column2':[2,4,5,2,3], 'cluster':[0,1,2,3,4]})
sns.relplot(x='column2', y='column1', hue='cluster', data=df, legend="full")
plt.show()

enter image description here

在此处输入图片说明

Categoricals

分类

An alternative is to make sure the values are treated categorical Unfortunately, even if you plug in the numbers as strings, they will be converted to numbers falling back to the same mechanism described above. This may be seen as a bug.

另一种方法是确保将值分类处理。不幸的是,即使您将数字作为字符串插入,它们也会被转换为回落到上述相同机制的数字。这可能被视为一个错误

However, one choice you have is to use real categories, like e.g. single letters.

但是,您的一种选择是使用真实的类别,例如单个字母。

'cluster':list("ABCDE")

works fine,

工作正常,

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

d = {'column1':[1,2,3,4,5], 'column2':[2,4,5,2,3], 'cluster':list("ABCDE")}

df = pd.DataFrame(data=d)

sns.relplot(x='column2', y='column1', hue='cluster', data=df)

plt.show()

enter image description here

在此处输入图片说明

Strings with customized palette

带有自定义调色板的字符串

An alternative to the above is to use numbers converted to strings, and then make sure to use a custom palette with as many colors as there are unique hues.

上面的替代方法是使用转换为字符串的数字,然后确保使用具有与独特色调一样多的颜色的自定义调色板。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

d = {'column1':[1,2,3,4,5], 'column2':[2,4,5,2,3], 'cluster':[1,2,3,4,5]}

df = pd.DataFrame(data=d)
df["cluster"] = df["cluster"].astype(str)

sns.relplot(x='column2', y='column1', hue='cluster', data=df, 
            palette=["b", "g", "r", "indigo", "k"])

plt.show()

enter image description here

在此处输入图片说明