简体   繁体   English

如何创建 Seaborn Pairplot 图例

[英]How to create a Seaborn Pairplot legend

This is my code for plotting some covid data for California and Texas.这是我为加利福尼亚和德克萨斯绘制一些 covid 数据的代码。 I have simply just taken my big dataframe ( df_covidNew ) and only looking at CA and TX.我只是拿走了我的大号 dataframe ( df_covidNew ),只查看了 CA 和 TX。

df_CA = df_covidNew[df_covidNew.state == "CA"]
df_TX = df_covidNew[df_covidNew.state == "TX"]

I then used a groupby() function to get the information that I want to plot in a pairplot:然后我使用groupby() function 来获取我想要的信息 plot 在 pairplot 中:

CA = df_CA.groupby(['month','month_name','state'])[['positive','negative','hospitalizedCurrently','death']].sum()
TX = df_TX.groupby(['month','month_name','state'])[['positive','negative','hospitalizedCurrently','death']].sum()

I then used concat to merge the two DataFrames together:然后我使用concat将两个 DataFrame 合并在一起:

df = pd.concat([CA,TX])

Using seaborn I tried to plot it, but it comes out as the same colour.使用 seaborn 我尝试使用 plot,但结果颜色相同。 There is no differentiation between CA and TX, which makes the plot pretty much useless. CA 和 TX 之间没有区别,这使得 plot 几乎毫无用处。

sns.pairplot(df)

This is what it looks like: PairPlot output这就是它的样子: PairPlot output

Is there a way I am able to differentiate between TX and CA in terms of a legend?有没有一种方法可以根据图例区分 TX 和 CA? I tried the legend method but it just says 'no legend handles'.我尝试了图例方法,但它只是说“没有图例句柄”。

Based on the seaborn pairplot documentation and structure of your DataFrame, I think you need to reset your MultiIndex into columns, then pass hue="state" :根据 seaborn pairplot 文档和 DataFrame 的结构,我认为您需要将 MultiIndex 重置为列,然后传递hue="state"

sns.pairplot(df.reset_index(), hue="state")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM