COSC131 guide to Matplotlib
Matplotlib is a library for making 2D plots in Python. It is Tweaks Axis ticks
designed with the philosophy that you should be able to
create simple plots with just a few commands: Change the colors, markers, line width and styles, etc.
1
xtick_values = [Link](0, 11, 2)
axes.set_xticks(xtick_values)
xs = [Link](0, 3*[Link], 100)
1 Initialize
ys = [Link](2*xs) 0
11 0 5
import numpy as np [Link](xs, ys, color="orange")
import [Link] as plt 0 2 4 6 8 10
xs = [Link](0, 3*[Link], 100)
2 Prepare ys = [Link](2*xs) 0 Multiple plots
xs = [Link](0, 10*[Link], 1000) [Link](xs, ys, linestyle="--")
11 You can plot several data on the the same figure, but you
xs = [Link](0, 3*[Link], 100) 0 5
ys = [Link](xs) can also split a figure in several subplots (named Axes):
3 Plot ys = [Link](2*xs) 0 1
1
xs = [Link](0, 2*[Link], 100)
0
[Link](xs, ys, linewidth=5)
axes = [Link]()
1
xs = [Link](0, 3*[Link], 100) 0 5
[Link](xs, [Link](2*xs))
[Link](xs, ys)
[Link]() 0
[Link](xs, [Link](2*xs))
1
0 5
ys = [Link](2*xs)
1
[Link](xs, ys, marker="o") figure, axes = [Link](2, 1)
4 Observe axes[0].plot(xs, [Link](2*xs))
1.0 Labels 0 5 axes[1].plot(xs, [Link](2*xs))
0.5
0.0 [Link](xs, ys) 1 Sine Legend
family="serif", color="red") 0
0.5
axes.set_title("Sine", size=20,
1
1.0
0 5 10 15 20 25 30 xs = [Link](0, 2*[Link], 100)
[Link](xs, [Link](2*xs), label="Sin")
[Link](xs, ys) 0 5 [Link](xs, [Link](2*xs), label="Cos")
Height
Other plots axes.set_ylabel("Height") [Link]()
Matplotlib offers several other kinds of plots. axes.set_xlabel("Time") axes.set_title("Sine and Cosine")
Time
For example:
Sine and Cosine
Grid
counts = [86, 50, 45, 65, 90] 80
60
[Link](range(0, 5), counts, 40
tick_label=["M","T","W","T","F"]) 20 xs = [Link](0, 10*[Link], 1000)
0
M T W T F [Link](xs, [Link](xs))
50
Sin
ys = [Link](0,1.5,200) [Link](True) Cos
0
[Link](ys, bins=range(-4, 5)) 1.0
2.524) 2.5
0.02.5
0.5
0.0
0.0
xs = [Link](0, 1.25,
0.5 Matplotlib 3.2 handout for beginners. Copyright (c) 2020 Nicolas P. Rougier. Re‐
ys = [Link](0, 1.25, 24)
2.5
1.0 leased under a CC‐BY International 4.0 License. Supported by NumFocus Grant
[Link](xs, ys, "o") 0 5 10 15 20 25 30 #12345. Adapted by Matthew Toohey and Richard Lobb for COSC131.
2.50.0