Facet plots in seaborn
Some plotting functions in seaborn such as histplot()
and lmplot()
have built-in facets. All you need to do is pass a col
and/or row
argument to create facets in your plot.
For functions that do not have built-in facets, you can manually create them with the FacetGrid()
function, and then specify the col
and/or row
to create your facets. To manually create a facetted plot, you can use the following code:
import seaborn as sns
import matplotlib.pyplot as plt
# Create a facet
facet = sns.FacetGrid(df, col='column_a', row='column_b')
# Generate a facetted scatter plot
facet.map(plt.scatter, 'column_x', 'column_y')
plt.show()
You can add another layer of data to the plot by using the hue
argument to color the points by a variable.
This exercise is part of the course
Python for R Users
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import seaborn as sns
import matplotlib.pyplot as plt
# Scatter plot of total_bill and tip faceted by smoker and colored by sex
sns.lmplot(x=____, y=____, data=tips, hue=____, col=____)
plt.show()