Tutorial 04 – Styling

maxplotlib passes matplotlib kwargs directly to the underlying renderer, so every colour, linestyle, marker, and alpha option you know from matplotlib works here too.

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

1 · Colors

Named colors, hex strings, and RGB tuples are all accepted.

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

canvas, ax = Canvas.subplots(width="10cm", ratio=0.6)

ax.plot(x, np.sin(x), color="steelblue", label="named: steelblue")
ax.plot(x, np.sin(x - 0.5), color="#e74c3c", label="hex: #e74c3c")
ax.plot(x, np.sin(x - 1.0), color=(0.2, 0.7, 0.3), label="RGB tuple")

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Color options")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_3_0.png

2 · Linestyles

Use long names ('solid', 'dashed', 'dotted', 'dashdot') or short aliases ('-', '--', ':', '-.').

[3]:
x = np.linspace(0, 4 * np.pi, 300)
styles = [("solid", "-"), ("dashed", "--"), ("dotted", ":"), ("dashdot", "-.")]

canvas, ax = Canvas.subplots(width="10cm", ratio=0.6)

for i, (name, ls) in enumerate(styles):
    ax.plot(
        x,
        np.sin(x) + i * 0.4,
        linestyle=ls,
        linewidth=2,
        color="steelblue",
        label=f"{name!r} / {ls!r}",
    )

ax.set_xlabel("x")
ax.set_title("Linestyle comparison")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_5_0.png

3 · Markers

Pass marker= to ax.plot or ax.scatter.

[4]:
x = np.linspace(0, 2 * np.pi, 10)
markers = ["o", "s", "^", "D", "*", "x", "+"]

canvas, ax = Canvas.subplots(width="10cm", ratio=0.65)

for i, m in enumerate(markers):
    ax.plot(
        x,
        np.sin(x) + i * 0.5,
        marker=m,
        linestyle="-",
        markersize=7,
        label=f"marker={m!r}",
    )

ax.set_xlabel("x")
ax.set_title("Marker comparison")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_7_0.png

4 · Linewidth and markersize

Control visual weight with linewidth= and markersize=.

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

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)

ax.plot(x, np.sin(x), linewidth=0.8, marker="o", markersize=3, label="thin / small")
ax.plot(x, np.sin(x) - 0.6, linewidth=2.5, marker="o", markersize=7, label="medium")
ax.plot(
    x, np.sin(x) - 1.2, linewidth=4.5, marker="o", markersize=12, label="thick / large"
)

ax.set_xlabel("x")
ax.set_title("Linewidth and markersize")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_9_0.png

5 · Alpha (transparency)

alpha= ranges from 0 (invisible) to 1 (opaque). Useful for overlapping fill_between regions.

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

canvas, ax = Canvas.subplots(width="10cm", ratio=0.55)

ax.fill_between(x, np.sin(x), 0, alpha=0.7, color="steelblue", label="alpha=0.7")
ax.fill_between(x, np.sin(2 * x), 0, alpha=0.4, color="tomato", label="alpha=0.4")
ax.fill_between(x, np.sin(3 * x), 0, alpha=0.2, color="seagreen", label="alpha=0.2")

ax.set_xlabel("x")
ax.set_title("Alpha transparency")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_11_0.png

6 · Combining styles – a publication-ready plot

Combine color, linestyle, marker, linewidth, and alpha in one plot.

[7]:
x = np.linspace(0, 2 * np.pi, 80)
noise = np.random.default_rng(0).normal(0, 0.05, len(x))

canvas, ax = Canvas.subplots(width="10cm", ratio=0.6)

# Shaded uncertainty band
ax.fill_between(x, np.sin(x) - 0.15, np.sin(x) + 0.15, alpha=0.15, color="steelblue")

# Noisy data
ax.scatter(
    x, np.sin(x) + noise, color="steelblue", marker="o", s=18, alpha=0.6, label="data"
)

# Clean model
ax.plot(
    x,
    np.sin(x),
    color="#e74c3c",
    linestyle="dashed",
    linewidth=2.0,
    alpha=0.9,
    label="model",
)

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Publication-style plot")
ax.set_legend(True)
canvas.show()
../_images/tutorials_tutorial_04_13_0.png

Summary

Option

Example

Named color

color='steelblue'

Hex color

color='#e74c3c'

RGB tuple

color=(0.2, 0.7, 0.3)

Linestyle

linestyle='dashed' or '--'

Marker

marker='o'

Linewidth

linewidth=2.0

Markersize

markersize=7

Transparency

alpha=0.5