Sie sind auf Seite 1von 1

Matplotlib for beginners

Matplotlib is a library for making 2D plots in Python. It is


Z = np.random.uniform(0, 1, (8,8) 765 Organize
designed with the philosophy that you should be able to
432 You can plot several data on the the same figure but you can
1
create simple plots with just a few commands:
ax.contourf(Z) also split a figure in several subplots (named Axes):
1 Initialize
Z = np.random.uniform(0, 1, 4) 765 1234567 765
432
X = np.linspace(0,10,100)
import numpy as np Y1, Y1 = np.sin(X), np.cos(X) 432
import matplotlib.pyplot as plt ax.pie(Z)
1 ax.plot(X, Y1, Y2)
1
Z = np.random.normal(0, 1, 100) 71
61
51 1234567 1234567
41 fig, (ax1, ax2) = plt.subplots((2,1))
2 Prepare
X = np.linspace(0, 4*np.pi, 1000) ax.hist(Z) 31
21
111
ax1.plot(X, Y1, color=”C1”)
ax2.plot(X, Y2, color=”C0”)
Y = np.sin(X)
X = np.arange(5) 765 1234567
432
fig, (ax1, ax2) = plt.subplots((1,2))
3 Render Y = np.random.uniform(0,1,5) ax1.plot(Y1, X, color=”C1”)
fig, ax = plt.subplots() ax.errorbar(X, Y, Y/4)
1 ax2.plot(Y2, X, color=”C0”)
ax.plot(X, Y)
Z = np.random.normal(0,1,(100,3)) 765 1234567
fig.show()
432 Label (everything)
4 Observe ax.boxplot(Z)
1 A Sine wave
1.0
246 ax.plot(X, Y)
fig.suptitle(None) 543
0.5
0.0
Tweak ax.set_title(”A Sine wave”) 21
0.5
1.0
You can modify pretty much anything in a plot, including lim-
its, colors, markers, line width and styles, ticks and ticks la-
ax.plot(X, Y) 1234567
ax.set_ylabel(None)
0 5 10 15 20 25 30 bels, titles, etc.
ax.set_xlabel(”Time”)

765
Time

Choose X = np.linspace(0,10,100)
Y = np.sin(X) 432 Explore
Matplotlib offers several kind of plots (see Gallery): ax.plot(X, Y, color=”black”)
1
765 765 1234567
Figures are shown with a graphical user interface that all-
X = np.random.uniform(0, 1, 100) X = np.linspace(0,10,100) lows to zoom and pan the figure, to navigate between the
Y = np.random.uniform(0, 1, 100) 432 Y = np.sin(X) 432 different views and to show the value under the mouse.
ax.scatter(X, Y)
1 ax.plot(X, Y, linestyle=”--”)
1
X = np.arange(10) 765 1234567 X = np.linspace(0,10,100) 765 1234567 Save (bitmap or vector format)
Y = np.random.uniform(1, 10, 10) 432 Y = np.sin(X) 432
ax.bar(X, Y)
1 ax.plot(X, Y, linewidth=5)
1 fig.savefig(”my-first-figure.png”, dpi=300)
fig.savefig(”my-first-figure.pdf”)
Z = np.random.uniform(0, 1, (8,8) 765 1234567 X = np.linspace(0,10,100) 765 1234567
432 Y = np.sin(X) 432
1 1
Matplotlib 3.2 handout for beginners. Copyright (c) 2020 Nicolas P. Rougier. Released
ax.imshow(Z) ax.plot(X, Y, marker=”o”) under a CC-BY International 4.0 License. Supported by NumFocus Grant #12345.

1234567 1234567

Das könnte Ihnen auch gefallen