-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy pathbasic-plots.py
154 lines (135 loc) · 5.35 KB
/
basic-plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -----------------------------------------------------------------------------
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
# Script to generate all the basic plots
import sys
import pathlib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
ROOT_DIR = pathlib.Path(__file__).parent.parent
sys.path.append(str(ROOT_DIR / "fonts"))
import custom_fonts # noqa
mpl.style.use([
ROOT_DIR / 'styles/base.mplstyle',
ROOT_DIR / 'styles/plotlet.mplstyle',
])
subplot_kw = dict(
xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8),
)
# Basic line plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
X = np.linspace(0, 10, 100)
Y = 4 + 2*np.sin(2*X)
ax.plot(X, Y, color="C1")
ax.grid()
fig.savefig(ROOT_DIR / "figures/basic-plot.pdf")
# Basic line plot (color)
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
X = np.linspace(0, 10, 100)
Y = 4 + 2*np.sin(2*X)
ax.plot(X, Y, color="black")
ax.grid()
fig.savefig(ROOT_DIR / "figures/basic-plot-color.pdf")
# Basic scatter plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(3)
X = 4 + np.random.normal(0, 1.25, 24)
Y = 4 + np.random.normal(0, 1.25, len(X))
ax.scatter(X, Y, 5, zorder=10,
edgecolor="white", facecolor="C1", linewidth=0.25)
ax.grid()
fig.savefig(ROOT_DIR / "figures/basic-scatter.pdf")
# Basic bar plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(3)
X = 0.5 + np.arange(8)
Y = np.random.uniform(2, 7, len(X))
ax.bar(X, Y, bottom=0, width=1,
edgecolor="white", facecolor="C1", linewidth=0.25)
ax.set_axisbelow(True)
ax.grid()
fig.savefig(ROOT_DIR / "figures/basic-bar.pdf")
# Basic imshow plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(3)
Z = np.zeros((8, 8, 4))
Z[:, :] = mpl.colors.to_rgba("C1")
Z[..., 3] = np.random.uniform(0.25, 1.0, (8, 8))
ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest")
ax.grid(linewidth=0.25, color="white")
fig.savefig(ROOT_DIR / "figures/basic-imshow.pdf")
# Basic pcolormesh plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(1)
X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256))
Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2)
Z = Z - Z.min()
plt.pcolormesh(X, Y, Z, cmap='Oranges', shading='auto')
ax.set_xlim(-3, 3), ax.set_xticks(np.arange(-3, 4))
ax.set_ylim(-3, 3), ax.set_yticks(np.arange(-3, 4))
fig.savefig(ROOT_DIR / "figures/basic-pcolormesh.pdf")
# Basic contour plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
colors = np.zeros((5, 4))
colors[:] = mpl.colors.to_rgba("C1")
colors[:, 3] = np.linspace(0.15, 0.85, len(colors))
plt.contourf(Z, len(colors), extent=[0, 8, 0, 8], colors=colors)
plt.contour(Z, len(colors), extent=[0, 8, 0, 8], colors="white",
linewidths=0.125, nchunk=10)
fig.savefig(ROOT_DIR / "figures/basic-contour.pdf")
# Basic pie plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
X = [1, 2, 3, 4]
colors = np.zeros((len(X), 4))
colors[:] = mpl.colors.to_rgba("C1")
colors[:, 3] = np.linspace(0.25, 0.75, len(X))
ax.set_axisbelow(True)
ax.grid(linewidth=0.25, color="0.75")
ax.pie(X, colors=["white"] * len(X), radius=3, center=(4, 4),
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
ax.pie(X, colors=colors, radius=3, center=(4, 4),
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
fig.savefig(ROOT_DIR / "figures/basic-pie.pdf")
# Basic text plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
ax.set_axisbelow(True)
ax.grid(linewidth=0.25, color="0.75")
ax.text(4, 4, "TEXT", color="C1", size=8, weight="bold",
ha="center", va="center", rotation=25)
fig.savefig(ROOT_DIR / "figures/basic-text.pdf")
# Basic fill plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(1)
X = np.linspace(0, 8, 16)
Y1 = 3 + 4*X/8 + np.random.uniform(0.0, 0.5, len(X))
Y2 = 1 + 2*X/8 + np.random.uniform(0.0, 0.5, len(X))
plt.fill_between(X, Y1, Y2, color="C1", alpha=.5, linewidth=0)
plt.plot(X, (Y1+Y2)/2, color="C1", linewidth=0.5)
ax.set_axisbelow(True)
ax.grid(color="0.75")
fig.savefig(ROOT_DIR / "figures/basic-fill.pdf")
# Basic quiver plot
# -----------------------------------------------------------------------------
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
np.random.seed(1)
T = np.linspace(0, 2*np.pi, 8)
X, Y = 4 + np.cos(T), 4 + np.sin(T)
U, V = 1.5*np.cos(T), 1.5*np.sin(T)
plt.quiver(X, Y, U, V, color="C1",
angles='xy', scale_units='xy', scale=0.5, width=.05)
ax.set_axisbelow(True)
ax.grid(color="0.75")
fig.savefig(ROOT_DIR / "figures/basic-quiver.pdf")