Repository navigation
Expand file tree
/
Copy pathfirst_figure.py
More file actions
346 lines (304 loc) · 9.82 KB
/
Copy pathfirst_figure.py
File metadata and controls
346 lines (304 loc) · 9.82 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.11.4
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [raw] raw_mimetype="text/restructuredtext"
# .. _first_figure:
#
# Your First UltraPlot Figure
# ===========================
#
# UltraPlot is designed to make creating complex Matplotlib figures simpler and
# more intuitive. This tutorial builds a two-panel figure in five stages.
# Because each stage is self-contained, you can run them individually while
# seeing exactly how UltraPlot removes standard Matplotlib boilerplate and
# automates layout generation.
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 1: Choose a layout
# ------------------------
#
# In standard Matplotlib, you generally have to guess a ``figsize`` tuple,
# which requires tedious trial and error anytime you add or remove subplots.
# UltraPlot calculates the figure size dynamically. By setting ``refwidth=2.4``,
# you simply define the width of a single reference panel, and UltraPlot
# automatically scales the entire figure to fit your rows and columns perfectly.
# %%
import ultraplot as uplt
fig, axs = uplt.subplots(ncols=2, share=False, refwidth=2.4)
fig.show()
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 2: Add data
# -----------------
#
# While UltraPlot revolutionizes the figure layout, the actual plotting
# commands are completely identical to Matplotlib. Because UltraPlot axes
# are directly built upon Matplotlib axes, you can use standard methods like
# :meth:`~ultraplot.axes.PlotAxes.plot` and
# :meth:`~ultraplot.axes.PlotAxes.imshow` with absolutely zero learning curve.
# %%
import numpy as np
import ultraplot as uplt
rng = np.random.RandomState(2024)
x = np.linspace(0, 2 * np.pi, 100)
signal = np.sin(x) + 0.06 * rng.randn(x.size)
image = np.outer(np.sin(x / 2), np.cos(x / 3))
fig, axs = uplt.subplots(ncols=2, share=False, refwidth=2.4)
axs[0].plot(x, signal, label="signal")
axs[1].imshow(image, origin="lower", aspect="auto")
fig.show()
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 3: Format the panels
# --------------------------
#
# This is where UltraPlot drastically reduces boilerplate code. In Matplotlib,
# formatting these two axes would require six separate lines of code (using
# :meth:`~matplotlib.axes.Axes.set_title`,
# :meth:`~matplotlib.axes.Axes.set_xlabel`, and
# :meth:`~matplotlib.axes.Axes.set_ylabel`). UltraPlot introduces the unified
# :meth:`~ultraplot.axes.Axes.format` method, allowing you to configure titles, labels,
# limits, and styling all in a single, readable function call per axis.
# %%
import numpy as np
import ultraplot as uplt
rng = np.random.RandomState(2024)
x = np.linspace(0, 2 * np.pi, 100)
signal = np.sin(x) + 0.06 * rng.randn(x.size)
image = np.outer(np.sin(x / 2), np.cos(x / 3))
fig, axs = uplt.subplots(ncols=2, share=False, refwidth=2.4)
axs[0].plot(x, signal, label="signal")
im = axs[1].imshow(image, origin="lower", aspect="auto")
axs[1].colorbar(im, label="Intensity", loc="r")
axs[0].format(title="Signal", xlabel="angle", ylabel="value")
axs[1].format(title="Image", xlabel="column", ylabel="row")
fig.show()
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 4: Add guides and figure formatting
# -----------------------------------------
#
# Adding colorbars and legends in Matplotlib is notorious for ruining layouts—they
# often overlap with data or require tedious :class:`~matplotlib.gridspec.GridSpec`
# wrangling. UltraPlot
# solves this natively. By passing simple location strings like ``loc="t"`` (top)
# or ``loc="r"`` (right) to :meth:`~ultraplot.figure.Figure.legend` and
# :meth:`~ultraplot.axes.Axes.colorbar`, UltraPlot allocates dedicated space
# *outside* the subplots without shrinking or distorting your axes.
# %%
import numpy as np
import ultraplot as uplt
rng = np.random.RandomState(2024)
x = np.linspace(0, 2 * np.pi, 100)
signal = np.sin(x) + 0.06 * rng.randn(x.size)
image = np.outer(np.sin(x / 2), np.cos(x / 3))
fig, axs = uplt.subplots(ncols=2, share=False, refwidth=2.4)
axs[0].plot(x, signal, label="signal")
axs[1].imshow(
image,
origin="lower",
aspect="auto",
colorbar="lr",
colorbar_kw=dict(label="Intensity"),
)
axs[0].format(title="Signal", xlabel="angle", ylabel="value")
axs[1].format(title="Image", xlabel="column", ylabel="row")
fig.format(suptitle="A first UltraPlot figure")
fig.legend(loc="b")
fig.show()
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 5: Save the result
# ------------------------
#
# Saving the figure is just as straightforward. Unlike standard Matplotlib, where
# you frequently have to pass ``bbox_inches="tight"`` to prevent labels and guides
# from being cut off, UltraPlot's automated layout engine guarantees that your
# saved file will automatically have perfectly tight margins.
# %%
import numpy as np
import ultraplot as uplt
rng = np.random.RandomState(2024)
x = np.linspace(0, 2 * np.pi, 100)
signal = np.sin(x) + 0.06 * rng.randn(x.size)
image = np.outer(np.sin(x / 2), np.cos(x / 3))
fig, axs = uplt.subplots(ncols=2, share=False, refwidth=2.4)
axs[0].plot(x, signal, label="signal")
axs[1].imshow(
image,
origin="lower",
aspect="auto",
colorbar="ur",
colorbar_kw=dict(label="Intensity"),
)
axs[0].format(title="Signal", xlabel="angle", ylabel="value")
axs[1].format(title="Image", xlabel="column", ylabel="row")
fig.format(suptitle="A first UltraPlot figure")
fig.legend(loc="b")
fig.save("first_figure.png")
# %% [raw] raw_mimetype="text/restructuredtext"
# Stage 6: The Grand Finale
# -------------------------
#
# To truly see UltraPlot's power, let's create a complex, publication-ready
# figure. In standard Matplotlib, combining a custom mosaic layout, a geographic
# projection, an inset axes, and A-B-C panel labels usually results in hundreds
# of lines of fragile :class:`~matplotlib.gridspec.GridSpec` and transform code.
#
# UltraPlot condenses all of this into a highly readable, declarative script.
# Notice how we assign a projection to just one panel using a dictionary,
# add an inset using a simple location string, and auto-generate our panel
# labels with a single ``abc=True`` argument.
# %%
import numpy as np
import ultraplot as uplt
# 1. Generate synthetic scientific data
rng = np.random.RandomState(2024)
lon, lat = np.linspace(-180, 180, 100), np.linspace(-90, 90, 100)
lon2d, lat2d = np.meshgrid(lon, lat)
# A pseudo-spatial anomaly pattern
geo_data = np.cos(np.radians(lat2d)) * np.sin(np.radians(lon2d * 2))
time = np.linspace(0, 10, 200)
series1 = np.sin(time) * np.exp(-time / 5)
series2 = np.cos(time) * np.exp(-time / 5)
# The map spans two rows while the right-hand panels stack beside it. With the
# 4:1 column ratio below, the Robinson map keeps its native 2:1 aspect and the
# right-hand panels receive approximately square plotting areas.
layout = [[1, 2], [1, 3]]
# 2. Create the figure
# Apply a Robinson projection only to the first panel.
fig, axs = uplt.subplots(
layout,
proj={1: "robin"},
share=0,
refnum=2,
refwidth=1.5,
wratios=(4, 1),
hspace="13em",
)
# 3. Geographic data
m = axs[0].contourf(
lon,
lat,
geo_data,
cmap="marine",
levels=15,
)
axs[0].format(
land=True,
borders=True,
labels=True,
lonlines=120,
latlines=45,
labelsize=12,
title="Global Spatial Anomaly",
title_kw={"fontsize": 14},
)
# A geographic callout is useful for showing a local-scale pattern without
# sacrificing the global view. These deterministic points mimic city readings.
paris = (2.3522, 48.8566)
paris_rng = np.random.RandomState(99)
paris_lon = paris[0] + paris_rng.normal(scale=0.65, size=36)
paris_lat = paris[1] + paris_rng.normal(scale=0.45, size=36)
paris_value = np.hypot(paris_lon - paris[0], paris_lat - paris[1])
paris_ax = axs[0].hawkeye(
(0.43, 0.68),
size=0.45,
anchor="ur",
proj="merc",
extent=(-0.8, 5.5, 46.8, 50.8),
shape="circle",
target="circle",
connector="line",
color="red7",
indicator_kw={"linewidth": 1.4},
)
paris_ax.format(land=True, landcolor="gray8", borders=True)
paris_ax.scatter(
paris_lon,
paris_lat,
c=paris_value,
cmap="fire",
markersize=24,
edgecolor="white",
linewidth=0.35,
transform="cyl",
absolute_size=True,
)
paris_ax.plot(
*paris,
marker="*",
markersize=9,
color="red7",
markeredgecolor="white",
markeredgewidth=0.6,
transform="cyl",
)
axs[0].colorbar(m, loc="b", label="Anomaly magnitude", length=0.8, labelsize=14)
# 4. Scatter data with inset
x = rng.rand(100)
y = x + rng.randn(100) * 0.2
axs[1].scatter(
x,
y,
c=x,
cmap="fire",
markersize=15,
alpha=0.7,
)
axs[1].format(
title="Correlation Profile",
xlabel="Predictor",
ylabel="Response",
xlocator=(0, 0.5, 1),
ylocator=(0, 0.5, 1),
xtickminor=False,
ytickminor=False,
ticklabelsize=10,
labelsize=12,
title_kw={"fontsize": 12},
)
ax_ins = axs[1].inset([0.55, 0.55, 0.35, 0.35], zoom=False)
ax_ins.hist(x, bins=10, color="gray5", edgecolor="black")
ax_ins.format(
titleloc="uc",
grid=False,
xtickminor=False,
ytickminor=False,
)
# 5. Time series
axs[2].plot(time, series1, label="Model Alpha")
axs[2].plot(time, series2, label="Model Beta")
axs[2].format(
title="Temporal Decay",
xlabel="Time (s)",
ylabel="Amplitude",
xlocator=(0, 5, 10),
ylocator=(-0.5, 0, 0.5, 1),
xtickminor=False,
ytickminor=False,
ticklabelsize=10,
labelsize=12,
title_kw={"fontsize": 12},
)
axs[2].legend(
loc="ur",
frame=False,
fontsize=6,
ncols=1,
)
# 6. Figure-wide formatting
fig.format(
suptitle="Putting It All Together",
suptitle_kw={"fontsize": 15},
abc=True,
abcloc="ul",
abcstyle="(a)",
abc_kw={"fontsize": 11},
)
fig.save("complex_figure.png", dpi=150)