简体   繁体   中英

Scatter plot grid faceted by columns in matplotlib or seaborn

I am trying to make a faceted scatterplot with Python (I know how I would've done this in R but I'm a newbie when it comes to Python...). I have a df like this fake one:

df=pd.DataFrame(
    [['Alex',10, 2, 2, 6],['Bob',12, 6, 5, 1],['Clark',13, 5, 3, 5]],columns=[
        'Name','Fruits', 'Apples', 'Bananas', 'Citrus'])  

and I would like to make a scatter plot with the relations between each fruit and the sum of fruits for each 'Name, ie Fruits and Apples, Fruits and Bananas, Fruits and Citrus.
For example the scatterplot with the relations between Fruits and Apples would be the one produced by this code:

plt.scatter(df[['Apples']], df[['Fruits']])  

Is there any convenient way of doing this?

Seaborn's FacetGrid is just what you're looking for. Seaborn usually works best with long-dfs too.

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

#create a long df, with columns for name, total fruit, and rows for specific fruit counts
df_melt = pd.melt(df, id_vars=['Name', 'Fruits'], value_vars=['Apples','Bananas','Citrus'], var_name='Fruit_Type', value_name='Count')

g = sns.FacetGrid(df_melt, col="Name",  col_wrap = 2, hue = "Fruit_Type")
g = g.map(plt.scatter, "Count", "Fruits")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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