Tutorial 05 – Axes, Ticks, Scales, and Annotations

This tutorial covers every way to control the coordinate frame of a subplot: labels, limits, tick marks, log scales, grids, legends, and annotations.

[1]:
from maxplotlib import Canvas
import numpy as np

1 · Labels and title

LaTeX strings are supported in all text arguments.

[2]:
x = np.linspace(0, 2 * np.pi, 200)

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)
ax.plot(x, np.sin(x), color="steelblue")

ax.set_xlabel(r"$x$ (radians)")
ax.set_ylabel(r"$\sin(x)$")
ax.set_title(r"The sine function $f(x) = \sin(x)$")
canvas.show()
../_images/tutorials_tutorial_05_3_0.png

2 · Axis limits

[3]:
x = np.linspace(0, 4 * np.pi, 300)

canvas, ax = Canvas.subplots(width="10cm", ratio=0.5)
ax.plot(x, np.sin(x), color="tomato")

# Show only the first full period
ax.set_xlim(0, 2 * np.pi)
ax.set_ylim(-1.2, 1.2)

ax.set_xlabel("x")
ax.set_ylabel(r"$\sin(x)$")
ax.set_title("Axis limits: first period only")
canvas.show()
../_images/tutorials_tutorial_05_5_0.png

3 · Custom ticks

Pass tick positions and optional labels to set_xticks.

[4]:
months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
]
temps = [3, 4, 7, 12, 17, 21, 23, 22, 18, 13, 7, 4]

canvas, ax = Canvas.subplots(width="12cm", ratio=0.45)
ax.plot(range(12), temps, marker="o", color="steelblue", linewidth=2)

ax.set_xticks(list(range(12)), labels=months)
ax.set_ylabel(r"Temperature ($^\circ$C)")
ax.set_title("Monthly average temperature")
canvas.show()
../_images/tutorials_tutorial_05_7_0.png

4 · Log scale

Use set_yscale('log') for data spanning several orders of magnitude. Compare linear and log in a 1×2 layout.

[5]:
x = np.linspace(0.1, 5, 200)
y = np.exp(2 * x)

canvas, (ax1, ax2) = Canvas.subplots(ncols=2, width="14cm", ratio=0.4)

ax1.plot(x, y, color="seagreen")
ax1.set_xlabel("x")
ax1.set_ylabel(r"$e^{2x}$")
ax1.set_title("Linear scale")

ax2.plot(x, y, color="seagreen")
ax2.set_xlabel("x")
ax2.set_yscale("log")
ax2.set_title("Log scale")

canvas.suptitle("Linear vs log scale")
canvas.show()
../_images/tutorials_tutorial_05_9_0.png

5 · Grid

[6]:
x = np.linspace(0, 2 * np.pi, 200)

canvas, (ax1, ax2) = Canvas.subplots(ncols=2, width="12cm", ratio=0.5)

ax1.plot(x, np.cos(x), color="steelblue")
ax1.set_title("No grid")
ax1.set_xlabel("x")

ax2.plot(x, np.cos(x), color="steelblue")
ax2.set_grid(True)
ax2.set_title("With grid")
ax2.set_xlabel("x")

canvas.show()
../_images/tutorials_tutorial_05_11_0.png

6 · Legend

Enable with set_legend(True). Labels come from label= kwargs.

[7]:
x = np.linspace(0, 2 * np.pi, 200)

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)
ax.plot(x, np.sin(x), color="steelblue", label=r"$\sin(x)$")
ax.plot(x, np.cos(x), color="tomato", label=r"$\cos(x)$")
ax.plot(x, np.sin(2 * x), color="seagreen", label=r"$\sin(2x)$", linestyle="dashed")

ax.set_xlabel("x")
ax.set_legend(True)
ax.set_title("Legend demo")
canvas.show()
../_images/tutorials_tutorial_05_13_0.png

7 · annotate

Draw an arrow from a text label to a data point.

[8]:
x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)
ax.plot(x, y, color="steelblue")

# Annotate the maximum
ax.annotate(
    r"maximum $\approx 1$",
    xy=(np.pi / 2, 1.0),
    xytext=(np.pi / 2 + 1.0, 0.7),
    arrowprops=dict(arrowstyle="->", color="black"),
    fontsize=9,
    color="darkred",
)

ax.set_xlabel("x")
ax.set_ylabel(r"$\sin(x)$")
ax.set_title("annotate demo")
canvas.show()
../_images/tutorials_tutorial_05_15_0.png

8 · text

Place a text label at arbitrary data coordinates.

[9]:
x = np.linspace(-2, 2, 200)

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)
ax.plot(x, x**2, color="darkorange")

ax.text(
    0, 3.2, r"$f(x) = x^2$", ha="center", va="bottom", fontsize=12, color="darkorange"
)

ax.set_xlabel("x")
ax.set_ylabel(r"$f(x)$")
ax.set_title("text demo")
canvas.show()
../_images/tutorials_tutorial_05_17_0.png

9 · Aspect ratio

set_aspect('equal') ensures a circle looks circular.

[10]:
theta = np.linspace(0, 2 * np.pi, 300)

canvas, ax = Canvas.subplots(width="7cm", ratio=1.0)
ax.plot(np.cos(theta), np.sin(theta), color="steelblue", linewidth=2)
ax.set_aspect("equal")
ax.set_title("Circle with equal aspect ratio")
ax.set_xlabel("x")
ax.set_ylabel("y")
canvas.show()
../_images/tutorials_tutorial_05_19_0.png

Summary

Method

Purpose

set_xlabel / set_ylabel

Axis labels (LaTeX OK)

set_title

Subplot title

set_xlim / set_ylim

Axis range

set_xticks(pos, labels=)

Custom tick marks

set_yscale('log')

Logarithmic scale

set_grid(True)

Background grid

set_legend(True)

Auto legend

annotate(...)

Arrow annotation

text(x, y, s)

Free text label

set_aspect('equal')

Equal x/y scaling