Things I Frequently Do in Matplotlib but Always Forget

Prerequisites

import matplotlib as mpl
import matplotlib.pyplot as plt

# Unless otherwise noted, the following are also used
fig = plt.figure()
ax = fig.add_subplot(111)

Making Plots Editable in Adobe

1. Use TrueType Font Type

The default is 3, which is Output Type. See this page for details.

mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['ps.fonttype'] = 42

2. Use the pgf Backend

This uses a LATEX processing system.

plt.savefig("test.pdf", backend="pgf")

Using Custom Fonts

On WSL, Windows fonts are available at /mnt/c/Windows/Fonts, and you often want to use them.

import matplotlib.font_manager as font_manager

fontpaths = ["/mnt/c/Windows/Fonts"]

font_files = font_manager.findSystemFonts(fontpaths=fontpaths)
for font_file in font_files:
    font_manager.fontManager.addfont(font_file)

plt.rcParams["font.family"] = "Arial"

Listing Available Fonts

import matplotlib.font_manager as font_manager

print(font_manager.fontManager.ttflist)

Controlling Margins

Useful when you have long labels and need to control margins.

# left, right, top, bottom
plt.rcParams['figure.subplot.left'] = 0.75

Colorbar-Only Plot

Use mpl.colorbar.Colorbar. ColorbarBase has been deprecated recently.

cmap = plt.get_cmap("gnuplot")
norm = mpl.colors.Normalize(vmin=0, vmax=10)

cbar = fig.add_subplot(111)
mpl.colorbar.Colorbar(
    cbar,
    mappable=mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
    orientation="vertical",
).set_label("label", fontsize=20)

Since mpl.colors.Normalize and the return value of plt.get_cmap are callable, you can obtain the normalized value and the corresponding color as follows:

value = 5
color = cmap(norm(value))

Drawing Shapes

Use patch.

import matplotlib.patch as patch

# Rectangle
ax.add_patch(patch.Rectangle(xy=(0, 0), width=10, height=10))
# Same color for border and fill (blue)
ax.add_patch(patch.Rectangle(xy=(0, 0), width=10, height=10, color="blue"))
# Different colors for border (black) and fill (blue)
ax.add_patch(patch.Rectangle(xy=(0, 0), width=10, height=10, edgecolor="black", facecolor="blue"))

Adjusting Tick Labels and Positions

ax = fig.add_subplot(111, xticks=[0, 0.25, 0.75, 1], xticklabels=["", "A", "B", ""])

Extracting a Size Legend Independently

Normally this is fine as-is, but when you adjust the size of points in a scatter plot, for example, you need to adjust the labels. In such cases, it's useful to extract the legend and redraw it.

actual_size = [0.01, 0.02, 0.03]
ax.scatter([1, 1, 1], [2, 2, 2], s=[s * 10 for s in actual_size])

handles, labels = ax.get_legend_handles_labels(prop="sizes", alpha=0.5)
legend = ax.legend(
    handles,
    labels,
    title="size",
    title_fontsize=15,
    markerscale=0.4
)

Adjusting Subplot Spacing Nicely

Use tight_layout.

fig = plt.figure()
ax1 = fig.add_subplot(112)
ax2 = fig.add_subplot(212)
fig.tight_layout()

Create an issue on GitHub about this article

Read Next