liveBook · Manning

Scatter plot

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A scatter plot displays individual observations as points positioned according to two numerical variables.

A scatter plot places each observation by two numeric values: one on the x axis and one on the y axis. It is a quick way to inspect the shape of a relationship, including whether values tend to rise together, fall in opposite directions, or show no clear pattern. section 11.12 section 13.3

With pandas, select the two DataFrame columns by name in `plot.scatter`. Longitude and latitude for populous US cities produce a recognizable map-like distribution, showing that the same technique can reveal spatial structure as well as statistical relationships. section 11.2

Use a scatter plot to explore a focused question before drawing conclusions:

  • Filter implausible or unwanted values first, as with taxi trips whose distance or total amount is outside a chosen range. section 11.12
  • Compare the visual pattern with `corr()` when correlation strength matters; the numeric result can be clearer than the plotted points alone. section 11.14
  • Treat an apparent association as a prompt for explanation, not proof that one measurement causes the other. section 11.14
  • Extend pairwise inspection to a scatter matrix when several numeric columns may be related. section 11.14

Color a taxi relationship by group

For a large taxi dataset, take a random 1% sample, then plot `trip_distance` on x and `total_amount` on y. With Seaborn, `hue='passenger_count'` colors points by passenger count, adding a third variable without changing the two axes. section 11.18

(
    institutions_df
    .plot.scatter(x='TUITIONFEE_OUT',
                  y='ADM_RATE',
                  c='MD_EARN_WNE_P10',
                  colormap='Spectral')
)

Plot tuition against admission rate and color each point by median graduate earnings with the Spectral colormap. section 13.3

Seaborn’s `relplot` is organized around relationships among numeric columns and draws a scatter plot by default. Use `x`, `y`, and `data`; then distinguish groups with `hue` and marker `style`, or split views into rows. section 11.16

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Works through pandas and Seaborn plotting calls, filtering, grouping, color encoding, and scatter matrices rather than plotting internals.
Examples
Uses city coordinates, weather temperatures, taxi trips, oil prices, ice cream, miles driven, and university tuition data.
What you can do after
Apply it via pandas or Seaborn to plot two numeric columns, filter observations, and encode groups with color or markers.
How it connects
Connects scatter plots to correlation, causation, scatter matrices, DataFrame joins, filtering, and Seaborn relplot.

Also explained in

Compare how 8 books explain scatter plot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.