Make charts with matplotlib. Copy any example into the Editor and press ▶ Run.
Import matplotlib's pyplot (we call it plt), build your chart, then
call plt.show() to display it. The first chart takes a moment while the charts
library loads — after that it is quick.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("Squares")
plt.show()
plt.show(). You can set a title and
axis labels with plt.title(...), plt.xlabel(...) and plt.ylabel(...).Give plot the x values and the y values.
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
temp = [12, 15, 14, 18, 20]
plt.plot(days, temp)
plt.title("Temperature this week")
plt.xlabel("Day")
plt.ylabel("°C")
plt.show()
Call plot again for each line, give each a label,
then add a legend so people know which is which.
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
london = [12, 15, 14, 18, 20]
paris = [14, 16, 16, 19, 22]
plt.plot(days, london, label="London")
plt.plot(days, paris, label="Paris", marker="o")
plt.title("Temperature")
plt.xlabel("Day")
plt.ylabel("°C")
plt.legend()
plt.show()
marker="o" (dots), linestyle="--"
(dashes) or a colour such as color="red".Use bar with labels and their values.
import matplotlib.pyplot as plt
fruits = ["apples", "bananas", "cherries"]
counts = [12, 19, 7]
plt.bar(fruits, counts, color="orange")
plt.title("Fruit basket")
plt.ylabel("How many")
plt.show()
Use barh for horizontal bars — handy when the labels are long.
import matplotlib.pyplot as plt
animals = ["cat", "dog", "rabbit", "hamster"]
votes = [8, 11, 5, 3]
plt.barh(animals, votes, color="seagreen")
plt.title("Favourite pet")
plt.xlabel("Votes")
plt.show()
Give pie the slice sizes and some labels. autopct
shows the percentage on each slice.
import matplotlib.pyplot as plt
sizes = [40, 25, 20, 15]
labels = ["Red", "Blue", "Green", "Yellow"]
plt.pie(sizes, labels=labels, autopct="%1.0f%%")
plt.title("Favourite colours")
plt.show()
explode=[0.1, 0, 0, 0] (one value per slice).For 3D you add a 3D axes with projection="3d", then draw on it.
You still finish with plt.show().
import matplotlib.pyplot as plt
ax = plt.axes(projection="3d")
t = range(0, 100)
x = [i / 10 for i in t]
y = [i / 15 for i in t]
z = [i / 20 for i in t]
ax.plot(x, y, z)
ax.set_title("A 3D line")
plt.show()
import matplotlib.pyplot as plt
import random
ax = plt.axes(projection="3d")
xs = [random.randint(0, 10) for _ in range(40)]
ys = [random.randint(0, 10) for _ in range(40)]
zs = [random.randint(0, 10) for _ in range(40)]
ax.scatter(xs, ys, zs, color="purple")
ax.set_title("Points in space")
plt.show()
Bars standing on a floor, each with a position (x, y) and a height.
bar3d takes the corner, the width/depth, and the height of each bar.
import matplotlib.pyplot as plt
ax = plt.axes(projection="3d")
xs = [0, 1, 2, 3] # left-right position
ys = [0, 1, 0, 1] # front-back position
heights = [3, 5, 2, 6] # how tall each bar is
ax.bar3d(xs, ys, [0, 0, 0, 0], 0.6, 0.6, heights, color="teal")
ax.set_title("3D bars")
plt.show()
These examples only scratch the surface. matplotlib can do a huge amount more — styles, colours, subplots, annotations and many chart types. Explore the official site and its gallery of examples: