Plotting With Python

Create line and scatter plots for quantitative data.

Learning Objectives

  • Plot a sequence of values.
  • Plot paired x/y values.
  • Interpret axes, markers, and trends.
  • Use reusable plotting helpers from FreeCampus Math.

Motivation

Graphs help us see patterns that are hard to notice in tables: growth, variation, outliers, and relationships between variables.

Line Plots

A line plot is useful when values have a natural order, such as time.

Line graph showing account balance increasing across five observations.

Scatter Plots

A scatter plot is useful for paired data.

Scatter plot showing expected return generally increasing as risk rises.

Reading a Plot

Ask three questions:

  1. What does the x-axis represent?
  2. What does the y-axis represent?
  3. What pattern do the points or line show?

Common Mistakes

Plotting Without Labels

Unlabeled axes make the graph hard to interpret. Always know what each axis means.

Assuming Correlation Means Causation

A scatter plot can show association, but it does not prove that one variable causes another.

Practice

  1. Plot [1, 4, 9, 16] as a line chart.
  2. Create a scatter plot for x = [1, 2, 3] and y = [3, 2, 5].
  3. Add a title that explains what the plot shows.
  4. Explain the pattern in one sentence.

Solutions

Answer:

from fcmath.python_fundamentals import plot_values

fig, ax = plot_values([1, 4, 9, 16], title="Square numbers")

Answer:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter([1, 2, 3], [3, 2, 5])

Answer:

ax.set_title("Observed values by input")

Answer: the values decrease from x = 1 to x = 2, then increase sharply at x = 3.

Back to top