1. Learn
  2. /
  3. Courses
  4. /
  5. Python for R Users

Connected

Exercise

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.

Instructions 1/2

undefined XP
    1
    2
  • Create a scatter plot of 'total_bill' on the x-axis and 'tip' on the y-axis with lmplot().
  • Facet the plot by 'smoker' and color the points by 'sex'.