Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Practice: Exercises, Cheat Sheet and Common Mistakes

Test your seaborn skills with graded exercises and solutions, a one-page cheat sheet of functions, and the most common mistakes and how to fix them.

You learn a plotting library by drawing with it. This final lesson gives you ten graded exercises, runnable solutions you can compare with your own, a cheat sheet that maps tasks to functions, and a checklist of the mistakes that cause most seaborn bug reports.

Work through the exercises in the order given. Try each one before opening the solution: the struggle is where the learning happens. Every exercise uses the same small dataset, defined below.

The shared dataset

Python Dataset for every exercise

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercises

Level 1: one plot

  1. Draw a histogram of sales with 25 bins and a KDE curve.
  2. Draw a count plot of store, ordered from most to least frequent.
  3. Draw a box plot of rating for each store.

Level 2: relationships 4. Scatter customers against sales, coloured by store, with a regression line per store. 5. Draw a heatmap of the mean sales for each store (rows) and month (columns), annotated.

Level 3: design and statistics 6. Show the mean sales per store with a 90% confidence interval, using the colour-blind palette. 7. Make a FacetGrid of sales histograms, one panel per store, with the median marked in each. 8. Build a pair plot of customers, sales and rating, hue by store, lower triangle only.

Level 4: put it together 9. Create a 1 by 3 figure: sales over time (monthly mean per store), a violin of rating per store, and a scatter of rating against customers with a logistic-free regression line. Give each panel a finding as its title. 10. Write a function report(df, store) that returns a figure of that store's sales histogram, rating box and monthly trend.

Solutions to 1 to 8

Python Solutions for exercises 1 to 5

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For 4, lmplot(hue="store", scatter_kws=...) is a one-liner but is figure-level, which cannot be placed into this grid. That is why the solution loops with regplot. Choose by the layout you need.

Python Solutions for exercises 6 to 8

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercises 9 and 10 are yours: they combine everything from the previous lessons. Use plt.subplots(1, 3, figsize=(15, 4)) for 9 and make report() return the fig rather than calling plt.show() inside, so the caller decides what to do with it.

Cheat sheet

Task Function
Distribution of one variable histplot, kdeplot, ecdfplot, displot
Two variables, both numeric scatterplot, lineplot, regplot, jointplot
Numeric by category boxplot, violinplot, stripplot, swarmplot, boxenplot
Category summary barplot, pointplot, countplot
Fit a line regplot (axes), lmplot (figure), residplot
Matrix of values heatmap, clustermap
Many variables pairplot, PairGrid
Split into panels FacetGrid, or col=/row= in figure-level functions
Look and feel set_theme, axes_style, set_context, despine, color_palette
Uncertainty errorbar=, estimator=, seed=, n_boot=
Legend placement sns.move_legend

Two lists to memorise:

  • Axes-level (accept ax=, return an Axes): scatterplot, lineplot, histplot, kdeplot, ecdfplot, barplot, boxplot, violinplot, stripplot, swarmplot, countplot, pointplot, regplot, residplot, heatmap.
  • Figure-level (own figure, accept col/row, height, aspect, return a grid): relplot, displot, catplot, lmplot, pairplot, jointplot, clustermap.

A tidy-data checklist

One row per observation Long form unlocks hue, col and row.
Real types Dates as datetime, categories ordered.
Clean first Handle NaN and impossible values.
Melt when wide One column per variable, not per value.

Seaborn works best with tidy data.

1 / 2

The ten most common mistakes

  1. figsize on a figure-level function. Use height and aspect. For subplots use plt.subplots(figsize=...) and an axes-level function.
  2. ax= on a figure-level function. It is not accepted. Use the returned grid (g.axes, g.ax_joint).
  3. plt.title() on a grid. It titles one axes. Use g.figure.suptitle() or g.set_titles().
  4. A palette that does nothing. From seaborn 0.13, palette needs a hue: pass hue=x and legend=False.
  5. Wide data into long-form arguments. Melt the DataFrame, or pass the frame without x/y for a quick wide-form plot.
  6. Dates as strings. The axis becomes categorical and the order alphabetical. Convert with pd.to_datetime.
  7. Reading barplot as a total. It shows the mean unless you set estimator="sum".
  8. Overplotting. Thousands of points become a blob: lower alpha, shrink s, or use hexbin/histplot/kdeplot.
  9. Misleading colour. Qualitative palettes for ordered values, rainbow maps for sequential data, red and green as the only contrast.
  10. Calling set_theme repeatedly or after plotting. Call it once at the top; use with sns.axes_style(...) for exceptions.

Where to go next

You have covered the whole library. To deepen it:

  • Revisit the basics in Matplotlib, since every seaborn figure is a Matplotlib figure underneath.
  • Learn data wrangling in pandas; good plots start with tidy tables.
  • Take the statistics further with statsmodels and SciPy.
  • Build a portfolio: three projects, each with a clear question, a tidy dataset and a written conclusion.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You need a violin plot placed in the second panel of a 1x3 figure. What do you use?

    violinplot is axes-level, so it accepts the Axes of the panel.
  2. 2. Why does palette='Set2' seem to be ignored in seaborn 0.13 when no hue is set?

    Since 0.13, palette is only applied when hue is mapped.
  3. 3. Which pair are both figure-level?

    catplot and lmplot create their own figure (a FacetGrid).
  4. 4. A scatter plot of 50,000 points is a solid blob. Which fix helps?

    Reduce overlap or show density instead of individual points.

Summary

  • Work through the exercises, then compare with the solutions and refine your own.
  • Know which functions are axes-level and which figure-level: it decides where size, titles and ax= go.
  • Tidy data, real dtypes and a thoughtful palette prevent most problems.
  • Practise with your own datasets and write one sentence of finding per chart.

Next: back to the Seaborn tutorial home