Reputation: 393
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, i.e. 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?
Upvotes: 3
Views: 3309
Reputation: 885
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")
Upvotes: 3