Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a Seaborn Pairplot legend

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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’.

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading