Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 76 ยท Visualization and Storytelling

Seaborn for Analysts: Count, Strip, Violin, KDE, Joint, Reg and Pair

Use Seaborn the analyst's way: pick count, strip, violin, KDE, joint, regression and pair plots by the question you are asking, and read each chart honestly.

Matplotlib for Analysts gave you full control over every line and label. Seaborn sits on top of Matplotlib and trades some of that control for speed: you give it a tidy DataFrame and column names, and one call draws a statistical chart, computing the densities, group summaries and fitted lines for you. For exploration, where you want twenty charts in an hour, this is a big win.

This lesson is not a catalogue of every Seaborn function. It organises the charts an analyst reaches for by the question being asked: how many of each, how spread out, how two numbers relate, and how many variables at once. For the full Seaborn tour, including themes and figure-level layouts, see Data Visualization with Seaborn. Here the emphasis is on choosing and reading the chart.

Setup

Seaborn is not part of the in-browser Python runner, so each example installs it with micropip (the first run needs an internet connection). At home you would run pip install seaborn and drop the first line. The built-in sample datasets are downloaded from the web, which the sandbox blocks, so every example builds its own data with a fixed random seed.

Python A small dataset to reuse

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.

Every example below recreates this DataFrame with the same seed (a short helper in each cell), so each runs on its own.

How many? Count plots

sns.countplot counts rows per category. It is a bar chart where Seaborn does the value_counts for you; hue splits each bar by a second category. Use it first on any categorical column to see the balance of classes.

Python countplot: how many of each?

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.

How spread out? Strip, violin and box plots

When you compare a number across groups, the average alone hides the shape. Three charts show more:

  • sns.stripplot draws every point, with a little random horizontal jitter so they do not overlap. Honest for small samples because nothing is summarised away.
  • sns.boxplot shows the median, quartiles and outliers.
  • sns.violinplot draws a smoothed density (a KDE) mirrored on both sides, so you see multiple peaks that a box plot would hide.

For a few dozen points use the strip plot; for hundreds, a violin or box plot; combine them (a strip plot over a violin) when you want shape and raw points together.

Python stripplot, violinplot and boxplot side by side

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.

cut=0 stops the violin from extending past the data range, which otherwise suggests negative spend. Always check that a smoothed shape does not claim values that are impossible.

What does the distribution look like? Histograms and KDE

sns.histplot draws a histogram, and kde=True overlays a smooth density curve. sns.kdeplot draws just the density, and hue compares groups. A kernel density estimate places a small bump at each data point and adds them up, so the bandwidth (smoothing) matters: too small gives a spiky curve, too large hides real peaks. Use bw_adjust to change it, and common_norm=False so every group's curve has the same area.

Python histplot and kdeplot with groups

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.

How do two numbers relate? Joint and regression plots

sns.jointplot puts a scatter plot (or hexbin or KDE) in the centre and the marginal distributions of each variable on the sides. It answers two questions at once: how does x relate to y, and what does each look like alone? Use kind="hex" or kind="kde" when there are too many points to see.

sns.regplot draws a scatter plot with a fitted regression line and a shaded confidence band (ci=95 by default). sns.lmplot does the same with hue or columns for groups. A fitted line is a summary of the trend, not a proof of causation; see Relationships Between Variables.

Python jointplot and regplot

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.

Many variables at once: pair plots

sns.pairplot draws a grid: the diagonal shows each variable's distribution, and every off-diagonal cell is a scatter plot of two variables. It is the fastest way to spot which pairs are related, which have outliers and whether groups (hue) separate. Keep it to about 5 to 6 numeric columns; beyond that the grid is unreadable. Use corner=True to draw only the lower triangle, since the upper one repeats it.

Python pairplot with hue

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.

Choosing by question

Question Chart Function
How many in each category? bar of counts countplot
What does one number look like? histogram, density histplot, kdeplot
How does a number differ across groups? points, summary, shape stripplot, boxplot, violinplot
How do two numbers relate? scatter with margins, trend jointplot, regplot
Which of many columns relate? scatter matrix pairplot
Correlations across many columns colour grid heatmap of df.corr()

Seaborn charts are for exploration first. When a chart goes to someone else, apply the polish from Visualization Principles and Communicating Results: a title that states the finding, labelled axes with units, and one message per chart. Because each Seaborn chart is a Matplotlib figure, every polishing step from the previous lesson still works.

A violin or KDE can look precise while resting on a handful of points, and a regression band can look tight on data that is mostly noise. Print the group sizes (value_counts) next to your charts and say how many rows each shape is built from.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You want to show every individual observation for three small groups without hiding them in a summary. Which Seaborn function fits?

    stripplot draws each point, with jitter to reduce overlap. A boxplot summarises the points, and countplot counts categories.
  2. 2. What does the bw_adjust argument of kdeplot control?

    bw_adjust scales the kernel bandwidth: values below 1 give a wigglier curve, above 1 a smoother one.
  3. 3. What extra information does jointplot give compared with a plain scatter plot?

    The histograms or densities along the top and right edges show how each variable is distributed on its own.
  4. 4. With 12 numeric columns, why is pairplot a poor choice?

    A pair plot grows with the square of the number of columns. Pick a handful of columns or use a correlation heatmap first.

Summary

  • Seaborn turns a tidy DataFrame and column names into a finished statistical chart; every chart is still a Matplotlib figure.
  • Choose by question: countplot (how many), histplot/kdeplot (distribution), stripplot/boxplot/violinplot (compare groups), jointplot/regplot (two numbers), pairplot (many numbers).
  • Check smoothing (bw_adjust, cut) and show group sizes so a smooth shape is not mistaken for a lot of data.
  • Use Seaborn for exploration, then polish the chart that you share.

Next: Interactive Charts with Plotly.