Skip to content
Analyzing and Visualizing Data

Scatterplots and Line Charts


Learning Objectives

  • You can use a scatterplot to inspect paired numeric measurements.
  • You can use a line chart to show change across an ordered coordinate.
  • You can preserve individual observations and explain what connecting them means.

The charts so far compared categories or displayed a distribution. A table can also contain two numeric measurements for each observation, or a measurement recorded at successive times. These questions need different displays. We will reuse the same Figure, Axes, labeling, and saving operations while changing what the marks represent.

Two Measurements per Journey

Suppose we record the distance and duration of each journey. A histogram of durations would show their distribution, but it would lose the pairing between a journey’s distance and its duration.

A scatterplot places each observation at its two measured coordinates. The horizontal position can represent distance and the vertical position duration. Both axes are numeric.

Create journey-measurements.csv in your data-analysis folder:

distance_km,minutes
0,0
1,12
2,18
2,25
3,22
4,35
5,40
6,38

These are invented records for practice. Each row is one journey. Distances are measured in kilometres and durations in minutes; repeated distances still belong to separate journeys. The first row records zero distance and zero duration. All measurements are present.

Replace analysis.py with:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
trips = pd.read_csv("journey-measurements.csv")
fig, ax = plt.subplots()
sns.scatterplot(data=trips, x="distance_km", y="minutes", ax=ax)
ax.set_title("Journey distance and duration")
ax.set_xlabel("Distance in kilometres")
ax.set_ylabel("Duration in minutes")
fig.savefig("journey-distance-duration.png", bbox_inches="tight")
plt.close(fig)

Run the program and open the saved PNG. The x and y arguments choose the two measurement columns. Unlike a histogram, the vertical coordinate is a recorded value rather than a count calculated by seaborn.

Scatterplot of eight journeys: the horizontal axis measures distance from 0 to 6 kilometres, and the vertical axis measures durations from 0 to 40 minutes. Two journeys at 2 kilometres have durations of 18 and 25 minutes.

Figure 1. Each point keeps a journey’s distance and duration together.

Read the point at (4, 35): that journey covered four kilometres and lasted 35 minutes. The two points above two kilometres have different durations. Longer journeys in this example generally took more time, but distance alone does not determine every duration.

The plot describes these records. It does not establish that changing distance caused the observed differences: travel mode, traffic, and other conditions were not recorded. Nor does it predict the duration of an unrecorded journey.

There is no recorded sequence between these independent journeys, so we leave their points unconnected. Joining them in CSV row order would make the file’s arrangement look meaningful. If two journeys have exactly the same coordinates, their points can overlap; the number of visible positions need not equal the number of records.

The next exercise supplies a table and an Axes, as in the earlier drawing exercises. Plot the paired measurements without grouping them. The caller handles the Figure and output file.

Journey Distance and Duration

0 / 30 points

Complete draw_journey_scatter(trips, ax). Draw each journey at its paired distance_km and minutes coordinates on the supplied ax. Use individual observations, including repeated coordinates and zeros; do not group them or connect journey points with lines.

The starter provides the title and axis labels in kilometres and minutes. Keep those lines and the full measured range visible.

The input is nonempty with nonnegative numeric measurements and no missing values. Leave trips unchanged. The caller saves the Figure; your function only draws.

Change across Study Weeks

A line chart connects observations in the order of a meaningful coordinate. Consider one student’s total study time recorded for each course week. The week numbers give an order that the journeys above did not have.

Create weekly-study.csv with these invented weekly records. The rows have deliberately been saved out of order:

week,minutes
4,100
1,60
6,90
2,80
5,120
3,75

Each row already contains one week’s total. There is one recorded value per week. We do not add these rows together before drawing.

Replace analysis.py with this example:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
weeks = pd.read_csv("weekly-study.csv")
ordered = weeks.sort_values("week")
fig, ax = plt.subplots()
sns.lineplot(data=ordered, x="week", y="minutes",
estimator=None, errorbar=None, sort=False, ax=ax)
ax.set_title("Recorded study time by week")
ax.set_xlabel("Study week")
ax.set_ylabel("Total time in minutes")
fig.savefig("weekly-study-time.png", bbox_inches="tight")
plt.close(fig)

Sorting prepares the observations by week rather than by their position in the file. sort=False tells lineplot to connect them in this prepared row order. Notice that we sort week, not minutes: ordering the durations would remove the rise and fall we want to examine. The returned table leaves weeks unchanged.

The lineplot call takes the same named-column arguments as the scatterplot. Here, estimator=None uses the supplied observations without calculating an average for repeated horizontal coordinates, and errorbar=None leaves out uncertainty intervals. Our input contract supplies exactly one value per week. A different file with repeated weeks would need its meaning checked before drawing.

Run the script and inspect Figure 2.

Line chart of weekly study totals, connecting weeks 1 to 6 in order with values 60, 80, 75, 100, 120, and 90 minutes.

Figure 2. The line connects recorded weekly totals in week order.

The total rises from 60 minutes in Week 1 to 80 in Week 2, falls to 75 in Week 3, and reaches its greatest recorded value of 120 in Week 5. Week 6 then records 90 minutes. These changes describe this student’s recorded study time.

The connecting segments help us follow the sequence. They do not record extra observations between weeks, prove why study time changed, or establish what Week 7 will contain. Keep the measured positions and values rather than smoothing the line or extending it beyond the records.

In the next exercise, the table’s rows can arrive in any order. Prepare them by week and draw one connected line on the supplied Axes. Keep the original table available to its caller.

Study Time by Week

0 / 30 points

Complete draw_weekly_minutes(weeks, ax). Draw one connected line through the recorded week and minutes coordinates on the supplied ax. Connect weeks from smallest to largest, regardless of input row order. Use only the recorded coordinates.

The starter provides the title and axis labels for study week and total minutes. Keep those lines and the full measured range visible.

The input has at least two rows, one per distinct positive integer week, with nonnegative numeric durations and no missing values. Leave weeks unchanged. The caller saves the Figure; your function only draws.

Check Your Understanding

  1. Two journeys have the same distance but different durations. How does a scatterplot preserve information that a histogram of durations would lose?
  2. Why would joining the journey points in CSV row order suggest a relationship the records do not provide?
  3. A weekly table is sorted by minutes before drawing. What happens to the question the line chart answers, and which column should determine its order?
  4. A line rises between two recorded weeks. What does that tell you about the recorded totals, and what does it leave unknown?