Generate figures for quick reference tables

Generate figures for quick reference tables#

This final section contains the code for figures used in the line properties table in the Matplotlib page.

import numpy as np
import matplotlib.pyplot as plt
# Machinery to store outputs for later use.
# This is for rendering in the Jupyter Book version of these pages.
from myst_nb import glue

Line property figures#

This example demonstrates using alpha for transparency:

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0.1, 1, 0.8), frameon=False)

for i in range(1, 11):
    plt.axvline(i, linewidth=1, color="blue", alpha=0.25 + 0.75 * i / 10.0)

plt.xlim(0, 11)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_alpha", fig, display=False)
../../_images/68468dfcf6c72757a01753cfffd4f94e6db7482f8d34ad8be150820d49f7a931.png

This example demonstrates aliased versus anti-aliased text.

First, aliased text (antialiased=False):

size = 128, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)

plt.axes((0, 0, 1, 1), frameon=False)

plt.rcParams["text.antialiased"] = False
plt.text(0.5, 0.5, "Aliased", ha="center", va="center")

plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xticks([])
plt.yticks([])

# Reset rcParams back to defaults
plt.rcdefaults()

# Store figure for use in reference table.
glue("plot_aliased", fig, display=False)
../../_images/dc7396feed4b28da00a5d4a58e73b6a765a3d02541cb1c16a3d5ff0042d50b4e.png

Next, antialiased=True.

fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

plt.rcParams["text.antialiased"] = True
plt.text(0.5, 0.5, "Anti-aliased", ha="center", va="center")

plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xticks([])
plt.yticks([])

# Reset rcParams back to defaults
plt.rcdefaults()

# Store figure for use in reference table.
glue("plot_antialiased", fig, display=False)
../../_images/10224f8e7d4a33eb1e71ee06f5103b36bad517c9fb9968495295c6ff69d70469.png

An example demoing the various colors taken by Matplotlib’s plot.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0.1, 1, 0.8), frameon=False)

for i in range(1, 11):
    plt.plot([i, i], [0, 1], lw=1.5)

plt.xlim(0, 11)
plt.xticks([])
plt.yticks([]);

# Store figure for use in reference table.
glue("plot_color", fig, display=False)
../../_images/6363e42fda9b248e147a9d073d10d55104f181dcdb2932f1385496534e17f49d.png

Plot various linewidths with Matplotlib.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0.1, 1, 0.8), frameon=False)

for i in range(1, 11):
    plt.plot([i, i], [0, 1], color="b", lw=i / 2.0)

plt.xlim(0, 11)
plt.ylim(0, 1)
plt.xticks([])
plt.yticks([]);

# Store figure for use in reference table.
glue("plot_linewidth", fig, display=False)
../../_images/5bfd00fa9fd25c1373a10320ff34a98995ad6ca76a8315d9d8ff2227dfd89d58.png

An example demoing the solid cap style in Matplotlib.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

plt.plot(np.arange(4), np.ones(4), color="blue", linewidth=8, solid_capstyle="butt")

plt.plot(
    5 + np.arange(4), np.ones(4), color="blue", linewidth=8, solid_capstyle="round"
)

plt.plot(
    10 + np.arange(4),
    np.ones(4),
    color="blue",
    linewidth=8,
    solid_capstyle="projecting",
)

plt.xlim(0, 14)
plt.xticks([])
plt.yticks([]);

# Store figure for use in reference table.
glue("plot_solid_capstyle", fig, display=False)
../../_images/a086c6434b30eaca8fb9b02ca28bd3d8a99717a10f5c57447d949a76ac612afe.png

An example showing the different solid joint styles in Matplotlib.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

plt.plot(np.arange(3), [0, 1, 0], color="blue", linewidth=8, solid_joinstyle="miter")
plt.plot(
    4 + np.arange(3), [0, 1, 0], color="blue", linewidth=8, solid_joinstyle="bevel"
)
plt.plot(
    8 + np.arange(3), [0, 1, 0], color="blue", linewidth=8, solid_joinstyle="round"
)

plt.xlim(0, 12)
plt.ylim(-1, 2)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_solid_joinstyle", fig, display=False)
../../_images/0ac2f88e885635ac5407d2d7757026a2524bf5779f40178b0fdc10618b43ad12.png

An example demoing the dash capstyle.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

plt.plot(
    np.arange(4),
    np.ones(4),
    color="blue",
    dashes=[15, 15],
    linewidth=8,
    dash_capstyle="butt",
)

plt.plot(
    5 + np.arange(4),
    np.ones(4),
    color="blue",
    dashes=[15, 15],
    linewidth=8,
    dash_capstyle="round",
)

plt.plot(
    10 + np.arange(4),
    np.ones(4),
    color="blue",
    dashes=[15, 15],
    linewidth=8,
    dash_capstyle="projecting",
)

plt.xlim(0, 14)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_dash_capstyle", fig, display=False)
../../_images/a086c6434b30eaca8fb9b02ca28bd3d8a99717a10f5c57447d949a76ac612afe.png

Example demoing the dash join style.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

plt.plot(
    np.arange(3),
    [0, 1, 0],
    color="blue",
    dashes=[12, 5],
    linewidth=8,
    dash_joinstyle="miter",
)
plt.plot(
    4 + np.arange(3),
    [0, 1, 0],
    color="blue",
    dashes=[12, 5],
    linewidth=8,
    dash_joinstyle="bevel",
)
plt.plot(
    8 + np.arange(3),
    [0, 1, 0],
    color="blue",
    dashes=[12, 5],
    linewidth=8,
    dash_joinstyle="round",
)

plt.xlim(0, 12)
plt.ylim(-1, 2)
plt.xticks([])
plt.yticks([]);

# Store figure for use in reference table.
glue("plot_dash_joinstyle", fig, display=False)
../../_images/09a44e839886d767db1e48e1802e502100d64856d657ddb5103abc667c1430e7.png

Demo the marker edge widths of Matplotlib’s markers.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

for i in range(1, 11):
    plt.plot(
        [
            i,
        ],
        [
            1,
        ],
        "s",
        markersize=5,
        markeredgewidth=1 + i / 10.0,
        markeredgecolor="k",
        markerfacecolor="w",
    )
plt.xlim(0, 11)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_mew", fig, display=False)
../../_images/222080ace830caeedb320febd16b7d2005cfe2d0cca0f31cc396e7f945064e73.png

Demo the marker edge color of Matplotlib’s markers.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

rng = np.random.default_rng()

for i in range(1, 11):
    r, g, b = np.random.uniform(0, 1, 3)
    plt.plot(
        [
            i,
        ],
        [
            1,
        ],
        "s",
        markersize=5,
        markerfacecolor="w",
        markeredgewidth=1.5,
        markeredgecolor=(r, g, b, 1),
    )

plt.xlim(0, 11)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_mec", fig, display=False)
../../_images/127ed0907fea9e09867cbb64d49b9051cf2a6daf5670a6736536b9ef54a57992.png

Demo the marker face color of Matplotlib’s markers.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

rng = np.random.default_rng()

for i in range(1, 11):
    r, g, b = np.random.uniform(0, 1, 3)
    plt.plot(
        [
            i,
        ],
        [
            1,
        ],
        "s",
        markersize=8,
        markerfacecolor=(r, g, b, 1),
        markeredgewidth=0.1,
        markeredgecolor=(0, 0, 0, 0.5),
    )
plt.xlim(0, 11)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_mfc", fig, display=False)
../../_images/f04b17c942ec94b9b6cdee18e4f7f010e4214b76a8ffc083dd18a05b9775ca57.png

Demo the marker size control in Matplotlib.

size = 256, 16
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)
plt.axes((0, 0, 1, 1), frameon=False)

for i in range(1, 11):
    plt.plot(
        [
            i,
        ],
        [
            1,
        ],
        "s",
        markersize=i,
        markerfacecolor="w",
        markeredgewidth=0.5,
        markeredgecolor="k",
    )

plt.xlim(0, 11)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference table.
glue("plot_ms", fig, display=False)
../../_images/b9d0629e9b7a7b32b52f565242bb22cabafdb91f65eb11e5bf6a047da2144e5e.png

Line styles figure#

def linestyle(ls, i):
    X = i * 0.5 * np.ones(11)
    Y = np.arange(11)
    plt.plot(
        X,
        Y,
        ls,
        color=(0.0, 0.0, 1, 1),
        lw=3,
        ms=8,
        mfc=(0.75, 0.75, 1, 1),
        mec=(0, 0, 1, 1),
    )
    plt.text(0.5 * i, 10.25, ls, rotation=90, fontsize=15, va="bottom")

linestyles = [
    "-",
    "--",
    ":",
    "-.",
    ".",
    ",",
    "o",
    "^",
    "v",
    "<",
    ">",
    "s",
    "+",
    "x",
    "d",
    "1",
    "2",
    "3",
    "4",
    "h",
    "p",
    "|",
    "_",
    "D",
    "H",
]
n_lines = len(linestyles)

size = 20 * n_lines, 300
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
plt.axes((0, 0.01, 1, 0.9), frameon=False)

for i, ls in enumerate(linestyles):
    linestyle(ls, i)

plt.xlim(-0.2, 0.2 + 0.5 * n_lines)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference sections.
glue("line_styles_fig", fig, display=False)
../../_images/01d99c36fc088267932df71a0276562c5d2c9600c2723fe58ccd6e0d55d0b594.png

Marker style figure#

def marker(m, i):
    X = i * 0.5 * np.ones(11)
    Y = np.arange(11)

    plt.plot(X, Y, lw=1, marker=m, ms=10, mfc=(0.75, 0.75, 1, 1), mec=(0, 0, 1, 1))
    plt.text(0.5 * i, 10.25, repr(m), rotation=90, fontsize=15, va="bottom")

markers = [
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    "o",
    "h",
    "_",
    "1",
    "2",
    "3",
    "4",
    "8",
    "p",
    "^",
    "v",
    "<",
    ">",
    "|",
    "d",
    ",",
    "+",
    "s",
    "*",
    "|",
    "x",
    "D",
    "H",
    ".",
]

n_markers = len(markers)

size = 20 * n_markers, 300
dpi = 72.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
plt.axes((0, 0.01, 1, 0.9), frameon=False)

for i, m in enumerate(markers):
    marker(m, i)

plt.xlim(-0.2, 0.2 + 0.5 * n_markers)
plt.xticks([])
plt.yticks([])

# Store figure for use in reference sections.
glue("marker_styles_fig", fig, display=False)
../../_images/f6e1faea3b94a327a54101ec1668bbd5136f876f6bd40b44b976f9530c1d8966.png

Colormap figure#

plt.rc("text", usetex=False)
a = np.outer(np.arange(0, 1, 0.01), np.ones(10))

fig = plt.figure(figsize=(10, 5))
plt.subplots_adjust(top=0.8, bottom=0.05, left=0.01, right=0.99)
maps = [m for m in plt.colormaps if not m.endswith("_r")]
maps.sort()
l = len(maps) + 1

for i, m in enumerate(maps):
    plt.subplot(1, l, i + 1)
    plt.axis("off")
    plt.imshow(a, aspect="auto", cmap=plt.get_cmap(m), origin="lower")
    plt.title(m, rotation=90, fontsize=10, va="bottom")

# Restore Matplotlib defaults.
plt.rcdefaults()

# Store figure for use in reference sections.
glue("colormap_fig", fig, display=False)
../../_images/8b5f27df1ee4d110cba7515b52ee4a1a32e37cc9430a7afffe900b5122f3daec.png