Matplotlib Plotting
plot() is Matplotlib's workhorse. By default it draws straight lines from point to point, but with a third argument, the format string, it can also draw only markers, or both.
Plotting x and y points
The first argument holds the x coordinates and the second the y coordinates. The two sequences must have the same length:
import numpy as np
import matplotlib.pyplot as plt
xs = np.array([0, 3, 5, 9])
ys = np.array([2, 8, 1, 6])
plt.plot(xs, ys)
plt.show()
for x, y in zip(xs, ys):
print(f"({x}, {y})")(0, 2)
(3, 8)
(5, 1)
(9, 6)
The line visits the points in the order they are given, not sorted by x. Try reversing xs and see that the picture stays the same, while shuffling only xs produces a zig-zag.
Plotting without a line
To show the points themselves, add a format string. "o" means "circle markers, no line":
import matplotlib.pyplot as plt
xs = [1, 2, 4, 7, 8]
ys = [3, 5, 4, 9, 7]
plt.plot(xs, ys, "o")
plt.show()
line = plt.gca().lines[0]
print("marker:", line.get_marker(), "| linestyle:", line.get_linestyle())marker: o | linestyle: None
The line style "None" confirms that no connecting line was drawn.
Format strings
A format string combines up to three parts, in any order: a colour, a marker and a line style.
| Part | Examples | Meaning |
|---|---|---|
| Colour | r g b k m c y w |
red, green, blue, black, magenta, cyan, yellow, white |
| Marker | o s ^ x + . * D |
circle, square, triangle, x, plus, point, star, diamond |
| Line | - -- : -. |
solid, dashed, dotted, dash-dot |
So "ro--" is a red dashed line with circle markers, and "k:" a black dotted line without markers.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1, 8)
plt.plot(x, x * 1.0, "b-")
plt.plot(x, x * 1.5, "gs--")
plt.plot(x, x * 2.0, "r^:")
plt.plot(x, x * 2.5, "ko")
plt.show()
for line in plt.gca().lines:
print(line.get_color(), line.get_marker(), line.get_linestyle())b None -
g s --
r ^ :
k o None
Default x values
If you give only one sequence, it is used as the y values, and x becomes 0, 1, 2, …, n-1:
import matplotlib.pyplot as plt
steps = [5, 9, 4, 12, 8, 10]
plt.plot(steps)
plt.show()
x_data = plt.gca().lines[0].get_xdata()
print("x values used:", x_data.tolist())x values used: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
Several datasets in one call
plot() accepts repeated x, y, fmt groups, so several lines can be drawn in a single call:
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 30)
lines = plt.plot(t, t, "c-", t, t ** 3, "m--")
plt.show()
print("plot() returned", len(lines), "Line2D objects")plot() returned 2 Line2D objects
plot() always returns a list of Line2D objects, one per dataset. Keep them if you want to change a line later, for example with lines[0].set_linewidth(3).
Tip: Format strings are a shortcut. For anything beyond the basics, use keyword arguments such as
color=,marker=andlinestyle=, which the next two lessons cover.
Test yourself
What does plt.plot(x, y, "o") draw?
- A line with circles
- Only circle markers, no line
- A solid line, no markers
- A pie chart
Which format string gives a green dashed line with square markers?
- "g--s"
- "gs-"
- "s:g"
- "green--"
What x values does plt.plot([4, 7, 2]) use?
- 1, 2, 3
- 0, 1, 2
- 4, 7, 2
- None, it raises an error