{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# TikzFigure Subplots Tutorial\n", "\n", "This tutorial demonstrates how to create side-by-side subplots using the `tikzfigure` backend.\n", "\n", "## Basic 1×2 Layout\n", "\n", "The simplest use case: two plots side by side." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "from maxplotlib import Canvas\n", "import numpy as np\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# Create a 1×2 canvas with custom width\n", "x = np.linspace(0, 2 * np.pi, 200)\n", "canvas, (ax1, ax2) = Canvas.subplots(ncols=2, width=\"10cm\", ratio=0.3)\n", "\n", "# Left subplot: sine wave\n", "ax1.plot(x, np.sin(x), color=\"royalblue\", label=\"sin(x)\", linewidth=1.5)\n", "ax1.set_title(\"sin(x)\")\n", "ax1.set_xlabel(\"x\")\n", "ax1.set_ylabel(\"amplitude\")\n", "\n", "# Right subplot: cosine wave\n", "ax2.plot(x, np.cos(x), color=\"tomato\", label=\"cos(x)\", linewidth=1.5)\n", "ax2.set_title(\"cos(x)\")\n", "ax2.set_xlabel(\"x\")\n", "\n", "# Set overall title\n", "canvas.suptitle(\"1 × 2 Layout: Trigonometric Functions\")\n", "\n", "# Render with tikzfigure backend\n", "result = canvas.show(backend=\"tikzfigure\")" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## 1×3 Layout: Multiple Functions\n", "\n", "You can create layouts with more than 2 subplots." ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# Create a 1×3 canvas\n", "canvas, axes = Canvas.subplots(ncols=3, width=\"12cm\", ratio=0.35)\n", "\n", "# Three different functions\n", "x = np.linspace(0, 2 * np.pi, 100)\n", "\n", "axes[0].plot(x, np.sin(x), color=\"blue\")\n", "axes[0].set_title(\"sin(x)\")\n", "axes[0].set_xlabel(\"x\")\n", "\n", "axes[1].plot(x, np.cos(x), color=\"red\")\n", "axes[1].set_title(\"cos(x)\")\n", "axes[1].set_xlabel(\"x\")\n", "\n", "axes[2].plot(x, np.tan(x), color=\"darkgreen\")\n", "axes[2].set_title(\"tan(x)\")\n", "axes[2].set_xlabel(\"x\")\n", "axes[2].set_ylim(-5, 5) # Limit y-range for tan\n", "\n", "canvas.suptitle(\"1 × 3 Layout: Three Trigonometric Functions\")\n", "result = canvas.show(backend=\"tikzfigure\")\n", "print(result)" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Important Notes\n", "\n", "- Only **horizontal layouts (1×n)** are supported with tikzfigure backend\n", "- Vertical/grid layouts (nrows > 1) will raise an error\n", "- Use matplotlib backend for complex layouts or grids\n", "- Each subplot's title becomes a subfigure caption in the LaTeX output" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }