Home > database >  How to create a Seaborn Pairplot legend
How to create a Seaborn Pairplot legend

Time:02-20

This is my code for plotting some covid data for California and Texas. I have simply just taken my big dataframe (df_covidNew) and only looking at CA and 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:

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:

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

Using seaborn I tried to plot it, but it comes out as the same colour. There is no differentiation between CA and TX, which makes the plot pretty much useless.

sns.pairplot(df)

This is what it looks like: PairPlot output

Is there a way I am able to differentiate between TX and CA in terms of a legend? I tried the legend method but it just says 'no legend handles'.

CodePudding user response:

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":

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