Lesson 04
Multiple Lines and a Legend
Draw several lines on one set of axes, label each one, and place a legend so a reader can tell the lines apart without asking.
Comparison is the whole point of most charts, and comparison needs more than one line. Call plot again and the second line lands on the same axes, in a new colour, sharing the same scale. The moment there are two lines, the reader needs a legend, and the legend comes from the label you give each line.
Call plot again
Each plot call adds a line to the current axes and picks the next colour in the default cycle. Nothing is replaced, so the axis range grows to fit whichever line goes highest.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
python = [42, 58, 51, 66, 73, 61, 47]
numpy = [30, 35, 44, 41, 52, 48, 39]
plt.plot(days, python)
plt.plot(days, numpy)
print("lines on these axes:", len(plt.gca().lines))
plt.show()Output:
lines on these axes: 2Label each line, then ask for the legend
label= names a line, and plt.legend() collects every labelled line into a box. A line with no label is skipped, which is a neat way to keep a faint reference line out of the legend.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
python = [42, 58, 51, 66, 73, 61, 47]
numpy = [30, 35, 44, 41, 52, 48, 39]
pandas_track = [18, 26, 31, 38, 47, 44, 36]
plt.plot(days, python, label="Python")
plt.plot(days, numpy, label="NumPy")
plt.plot(days, pandas_track, label="pandas")
plt.title("Signups by track")
plt.xlabel("day")
plt.ylabel("signups")
plt.legend()
plt.show()
for name, values in [("Python", python), ("NumPy", numpy), ("pandas", pandas_track)]:
print(f"{name:<8} total {sum(values)}")Output:
Python total 398
NumPy total 289
pandas total 240Where the legend sits
loc moves the legend. The default is "best", which hunts for the emptiest corner, and that is usually right. Name a corner yourself when you want the position to stay put across several charts, and give the box a title when the labels share a theme.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
python = [42, 58, 51, 66, 73, 61, 47]
numpy = [30, 35, 44, 41, 52, 48, 39]
plt.plot(days, python, label="Python")
plt.plot(days, numpy, label="NumPy")
plt.legend(loc="upper left", title="Track")
plt.show()
print("some choices:", ", ".join(["best", "upper left", "lower right", "center"]))Output:
some choices: best, upper left, lower right, centerSeparate charts instead of one
show ends the current figure. Calling it between plots gives you two separate charts rather than two lines on one, which is what you want when the two measurements have nothing to do with each other or use wildly different scales.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
revenue = [1840, 2510, 2260, 2980, 3310, 2740, 2090]
plt.plot(days, signups)
plt.title("Signups")
plt.show()
plt.plot(days, revenue)
plt.title("Revenue")
plt.show()
print("plotting these together would flatten the signups line")Output:
plotting these together would flatten the signups lineBoth charts appear one after the other in the playground. Sharing one axis here would squash the signups line against the bottom, because revenue is fifty times larger.
Key takeaways
-
Each plot() call adds another line to the same axes
-
label= names a line and plt.legend() collects the labelled ones
-
loc moves the legend; the default 'best' finds an empty corner
-
Call show() between plots to get separate figures
-
Next: Line Styles and Colours
-
Previous: Your First Line Plot
Loading playground…
Check your understanding
Multiple Lines and a Legend Quiz
What happens on the second plot() call?
1 / 10