作者:手机用户2502875927 | 来源:互联网 | 2022-12-09 16:00
hue参数跳过一个整数.
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)
Python 2.7 Seaborn 0.9.0 Ubuntu 16.04 LTS
1> ImportanceOf..:
"完整"的传奇
如果hue
是数字格式,seaborn将假定它代表一些连续数量,并决定显示它认为是沿着颜色维度的代表性样本.
您可以通过使用来规避这一点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()
Categoricals
另一种方法是确保值被分类处理不幸的是,即使您将数字作为字符串插入,它们也会转换为回退到上述相同机制的数字.这可能被视为一个错误.
但是,您有一个选择是使用真实的类别,例如单个字母.
'cluster':list("ABCDE")
工作良好,
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()
字符串与自定义调色板
上面的替代方法是使用转换为字符串的数字,然后确保使用具有与唯一色调一样多的颜色的自定义调色板.
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()