Common figure recipes#
These small, complete examples cover the patterns most figures need. Copy a recipe into a script or notebook and adapt the data and labels.
[1]:
import numpy as np
import ultraplot as uplt
Labelled line#
Use label on each artist and format the axes in one place.
[2]:
x = np.linspace(0, 2 * np.pi, 200)
fig, ax = uplt.subplots(refwidth=3.2)
ax.plot(x, np.sin(x), label="sine")
ax.plot(x, np.cos(x), label="cosine")
ax.format(xlabel="angle", ylabel="value", title="Two signals")
ax.legend(loc="ur")
[2]:
<ultraplot.legend.Legend at 0x77a6c0f0fb60>
Image with a colorbar#
Plot the returned mappable and request an outer colorbar with
colorbar().
[4]:
image = np.outer(np.sin(x[:60]), np.cos(x[:60]))
fig, ax = uplt.subplots(refwidth=3.0)
ax.imshow(image, cmap="viridis", colorbar="r")
ax.format(title="Image data", xformatter="none", yformatter="none")
Figure-wide legend#
A legend() collects labelled artists across
the selected axes.
[5]:
fig, axs = uplt.subplots(ncols=2, refwidth=2.2)
for ax, phase in zip(axs, (0, np.pi / 2)):
ax.plot(x, np.sin(x + phase), label="signal")
ax.plot(x, np.cos(x + phase), label="reference")
ax.format(title=f"phase = {phase:.2g}")
fig.legend(loc="b", ncols=2)
[5]:
<ultraplot.legend.Legend at 0x77a6b6a6aad0>
Publication-sized export#
Set a physical figure width and write a vector file for a manuscript with
save().
[6]:
fig, ax = uplt.subplots(figwidth="89mm", refaspect=1.6)
ax.plot(x, np.sin(x), color="C0")
ax.format(xlabel="angle", ylabel="value")
fig.save("figure.pdf")