Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Relational and Categorical Plots

Scatter Plots with scatterplot

Draw scatter plots with seaborn scatterplot and relplot: read relationships and correlation, color by group, handle overplotting, add facets and a regression line.

A scatter plot puts one variable on each axis and draws a dot per observation. It is the standard way to ask: do these two numeric variables move together? sns.scatterplot makes that one call and lets you add up to three more variables through color, size and marker shape. This lesson also covers overplotting, faceting and the figure-level relplot.

The basic scatter plot

Python A scatter plot 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.

Reading it: the cloud slopes upward, so larger bills come with larger tips (the printed correlation is high). Color separates lunch and dinner, and marker shape separates smokers. A scatter plot shows the form of a relationship (straight, curved, clustered), the strength (tight or loose) and outliers. Correlation does not prove one variable causes the other.

Useful arguments

Argument Effect
hue, style, size Map columns to color, marker shape and marker area
s=60 Fixed marker area (not a column)
alpha=0.5 Transparency, so overlapping points show up darker
edgecolor="white", linewidth=0.5 Thin outlines that separate touching points
marker="o" Single marker shape when you do not need style
palette, hue_order Colors and level order
legend="brief" or False Legend control
ax= Draw on an axes you created

Overplotting

When many points share nearly the same position, the dots hide each other. A few fixes, from simplest up:

  1. alpha=0.3 makes dense areas darker.
  2. A smaller s and no outline: s=10, linewidth=0.
  3. Switch to a 2D histogram or KDE, as in Bivariate Distributions.
  4. For discrete values (such as party size on an axis), consider a strip or swarm plot.
Python Fixing overplotting with alpha and size

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.

relplot: facets for free

sns.relplot(kind="scatter") is the figure-level version. It supports col= and row= for small multiples, handy when hue would produce too many colors.

Python relplot with col and row

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.

Each panel shares axes, so you can compare slopes and spreads directly. This is the "small multiples" idea: repeat the same chart, change one thing at a time. See Facet Grids for the full toolkit.

Adding a trend

scatterplot draws only points. To add a fitted line, use regplot or lmplot, which are covered in Regression Plots. For a quick check that does not need a model, overlay a lineplot of group averages.

Scatter plots on a log scale

For data spanning several orders of magnitude (populations, prices), call ax.set(xscale="log", yscale="log"). A straight line on log-log axes suggests a power-law relationship.

Python
ax = sns.scatterplot(data=df, x="population", y="gdp")
ax.set(xscale="log", yscale="log")

Common mistakes

  • Concluding causation from a trend.
  • Mapping too many variables at once, hiding the main message.
  • Forgetting alpha with thousands of points.
  • A categorical string column on an axis, which makes points land at category positions rather than on a numeric scale.
  • Using plt.scatter with hue: that is a seaborn feature, not a Matplotlib one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a scatter plot primarily show?

    Each dot is one observation positioned by two numeric values.
  2. 2. Which argument makes overlapping points easier to see?

    Transparency makes dense regions darker, revealing where points pile up.
  3. 3. How do you draw a separate scatter panel for each value of 'time'?

    relplot is the figure-level function and supports col and row.
  4. 4. A scatter plot of two variables slopes upward. What can you correctly say?

    A pattern is evidence of association only.

Summary

  • scatterplot(data=, x=, y=) shows the relationship between two numeric variables; hue, style and size add more.
  • Fight overplotting with alpha, smaller markers, or a 2D density plot.
  • relplot(kind="scatter", col=, row=) makes small multiples.
  • Use regplot or lmplot for fitted lines and log axes for wide ranges.
  • A visible pattern shows association, not cause.

Next: Bubble Plots: Adding a Third Variable with Size