Skip to content Skip to sidebar Skip to footer

How To Add A Comparison Line To All Plots When Using Seaborn's Facetgrid

I'm trying to add the same comparison line to multiple plots using FacetGrid. Here is where I get stuck: # Import the dataset tips = sns.load_dataset('tips') # Plot using FaceGri

Solution 1:

You can map the same function to the FacetGrid.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Import the dataset
tips = sns.load_dataset("tips")

# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", height=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")

defconst_line(*args, **kwargs):
    x = np.arange(0, 50, .5)
    y = 0.2*x
    plt.plot(y, x, C='k')

g.map(const_line)

plt.show()

Solution 2:

Another indirect way is to get the axes object from the FacetGrid and then plot the line to each of them

g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")

axes = g.fig.axes
x = np.arange(0, 50, .5)
y = 0.2*x
for ax in axes:
    ax.plot(y, x, C='k')
plt.show()

enter image description here

Post a Comment for "How To Add A Comparison Line To All Plots When Using Seaborn's Facetgrid"