Lesson 03
Your First Line Plot
Draw line charts with plot, understand what happens when you pass only one list, and add the title and axis names that make a chart readable.
A line plot draws each pair of values as a point and joins neighbouring points with a straight segment. That joining line is a claim: it says the x axis has an order, and that moving along it means something. Days, months, distances, and temperatures qualify. Unordered categories such as fruit or city names do not.
Two lists of the same length
plot(x, y) pairs the lists position by position: the first x with the first y, and so on. They must be the same length, and if they are not, Matplotlib says so rather than guessing.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
print(len(days), len(signups))
try:
plt.plot(days, signups[:3])
except ValueError as problem:
print("ValueError:", problem)
plt.plot(days, signups)
plt.show()Output:
7 7
ValueError: x and y must have same first dimension, but have shapes (7,) and (3,)One list is allowed
Pass a single list and Matplotlib treats it as the y values, using 0, 1, 2, and so on for x. That is handy for a quick look, but the x axis then counts positions rather than anything real, so label it honestly or pass proper x values.
import matplotlib.pyplot as plt
signups = [42, 58, 51, 66, 73, 61, 47]
line, = plt.plot(signups)
xs, ys = line.get_data()
print("x values Matplotlib invented:", list(xs))
print("y values you gave:", list(ys))
plt.show()Output:
x values Matplotlib invented: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
y values you gave: [42, 58, 51, 66, 73, 61, 47]plot returns a list of the lines it drew, which is why the comma in line, = plt.plot(...) unpacks the single line out of it.
Label it before you share it
Three lines of text turn a shape into a finding. Write the title as the sentence you want the reader to take away, and name both axes including the unit when there is one.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
plt.plot(days, signups)
plt.title("Signups peak on Friday")
plt.xlabel("day of the week")
plt.ylabel("new signups")
plt.show()
print("peak:", max(signups), "on", days[signups.index(max(signups))])Output:
peak: 73 on FriWhen a line lies
The line between two points suggests the values in between. For days that is fair, because Tuesday really does sit between Monday and Wednesday. For unordered categories it invents a trend that does not exist, and a bar chart is the honest choice.
import matplotlib.pyplot as plt
tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]
# Misleading: there is no path from Python to NumPy.
plt.plot(tracks, enrolled)
plt.title("Wrong chart for this data")
plt.show()
# Honest: four separate comparisons.
plt.bar(tracks, enrolled)
plt.title("Learners per track")
plt.show()
print("gap between the largest and smallest:", max(enrolled) - min(enrolled))Output:
gap between the largest and smallest: 62Key takeaways
-
plot(x, y) pairs the two lists position by position and joins the points
-
Passing one list uses 0, 1, 2 as the x values
-
Give every chart a title and a name for each axis
-
Lines imply order; use bars for unordered categories
-
Previous: Anatomy of a Figure
Loading playground…
Check your understanding
Your First Line Plot Quiz
How does plot() pair two lists?
1 / 10