【问题标题】:hue in seaborn catplotseaborn catplot 中的色调
【发布时间】:2018-09-25 22:47:19
【问题描述】:

我想绘制一个包含多列的 seaborn catplot,并使用色调来区分一些观察结果。

假设我有

import pandas as pd
import seaborn as sns
import random as r
import numpy as np

name_list=['pepe','Fabrice','jim','Michael']
country_list=['spain','France','uk','Uruguay']
favourite_color=['green','blue','red','white']

df=pd.DataFrame({'name':[r.choice(name_list) for n in range(100)],
         'country':[r.choice(country_list) for n in range(100)],
         'fav_color':[r.choice(favourite_color) for n in range(100)],
         'score':np.random.rand(100),})

如果我跑步

sns.catplot(y='score',hue='fav_color',col='country',data=df)

我得到一个 TypeError: 'NoneType' object is not iterable。如果我指定legend=False,我不会得到任何错误,但所有点看起来都是相同的颜色。我还尝试指定具有相同结果的调色板。这是一个错误还是我做错了什么?

【问题讨论】:

  • 我认为您忘记指定要在 xaxis 上显示的内容。在这种情况下,也许是名字? sns.catplot(x="name", y='score',hue='fav_color',col='country',data=df)
  • 我明白了...我没有指定 x,因为我想要每列一个...我正在绘制的内容具有不同的单位,我想使用 sharey=False。也许最好用 FacetGrid 做 4 个不同的带状图

标签: python python-3.x seaborn


【解决方案1】:

您可以尝试使用不同的选项(如点或条等)将 X 作为色调值。

    sns.catplot(y='score', x='fav_color',col='country',data=df, kind='point')
    sns.catplot(y='score', x='fav_color',col='country',data=df)

指定 kind = 'count' 没有像这样的 X 变量

    sns.catplot(y='score',hue='fav_color',col='country',data=df,kind='count')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2019-01-20
    • 2019-11-10
    • 2023-01-17
    • 1970-01-01
    • 2021-05-12
    • 2020-02-17
    相关资源
    最近更新 更多