Tutorial 01 — Quick Start

maxplotlib is a thin, expressive wrapper around Matplotlib that simplifies creating publication-quality figures. Its central class, Canvas, replaces the usual fig, ax = plt.subplots() boilerplate and exposes a clean, chainable API.

This notebook walks you through the very basics:

  • creating a canvas and a subplot

  • adding lines

  • using the canvas-level shortcut API

  • saving figures

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

%matplotlib inline

1 Minimal example

Canvas.subplots() mirrors plt.subplots(). It returns the canvas and one (or more) subplot axes objects.

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

canvas, ax = Canvas.subplots()
ax.plot(x, y)
canvas.show()
../_images/tutorials_tutorial_01_3_0.png

2 Multiple lines with labels, colors, and linestyles

[3]:
canvas, ax = Canvas.subplots()

ax.plot(x, np.sin(x), label="sin(x)", color="royalblue", linestyle="solid", linewidth=2)
ax.plot(x, np.cos(x), label="cos(x)", color="tomato", linestyle="dashed", linewidth=2)
ax.plot(
    x,
    np.sin(2 * x),
    label="sin(2x)",
    color="forestgreen",
    linestyle="dotted",
    linewidth=1.5,
)

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Sine and Cosine")
ax.set_legend(True)

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

3 Canvas-level shortcut API

For single-subplot figures you can call plot methods directly on the Canvas object — they are forwarded to the subplot at row=0, col=0.

[4]:
canvas = Canvas(ratio=0.5, fontsize=12)

canvas.add_line(x, np.sin(x), label="sin(x)", color="steelblue")
canvas.add_line(x, np.cos(x), label="cos(x)", color="darkorange", linestyle="dashed")

canvas.set_xlabel("angle (rad)")
canvas.set_ylabel("amplitude")
canvas.set_title("Using canvas-level methods")
canvas.set_legend(True)
canvas.set_grid(True)

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

4 Configuring the subplot at creation time

add_subplot() accepts convenience kwargs so you can set labels, grid, and legend in one call.

[5]:
canvas = Canvas(ratio=0.5)
ax = canvas.add_subplot(
    title="Configured at creation",
    xlabel="x",
    ylabel="f(x)",
    grid=True,
    legend=True,
)

ax.plot(x, np.sin(x), label="sin", color="royalblue")
ax.plot(x, x / (2 * np.pi), label="x/2π", color="coral", linestyle="dashed")

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

5 Saving a figure

Use canvas.savefig() to write the figure to disk. The file extension determines the format; pass backend='matplotlib' for PDF output.

[6]:
canvas = Canvas(ratio=0.5)
ax = canvas.add_subplot(xlabel="x", ylabel="sin(x)", grid=True)
ax.plot(x, np.sin(x), color="steelblue")

canvas.savefig("tutorial_01_output.png")
print("Figure saved to tutorial_01_output.png")
Figure saved to tutorial_01_output.png
../_images/tutorials_tutorial_01_11_1.png

Summary

Task

Code

Create canvas + subplot

canvas, ax = Canvas.subplots()

Add a line

ax.plot(x, y, label=..., color=..., linestyle=...)

Canvas shortcut

canvas.add_line(x, y, ...)

Labels / title

ax.set_xlabel(), ax.set_ylabel(), ax.set_title()

Legend / grid

ax.set_legend(True), ax.set_grid(True)

Display

canvas.show()

Save

canvas.savefig('out.png')

Continue to Tutorial 02 to learn about multi-subplot layouts.