{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Tutorial 14 – Axis and layout controls\n", "\n", "This tutorial demonstrates the latest Matplotlib-style wrappers: filled polygons, logarithmic shortcuts, axis limits and autoscaling, secondary axes, box aspect, and explicit tick labels." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from maxplotlib import Canvas\n", "\n", "x = np.linspace(0.1, 10, 200)" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Filled polygons\n", "\n", "`fill()` forwards polygon coordinates to Matplotlib and becomes a filled Plotly scatter trace." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "canvas = Canvas()\n", "canvas.fill([0, 1, 2, 1], [0, 2, 0, -1], color=\"tab:orange\", alpha=0.5)\n", "canvas.set_title(\"A filled polygon\")\n", "canvas.show()" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## Logarithmic plotting shortcuts\n", "\n", "Use `semilogx()`, `semilogy()`, or `loglog()` when the data and axis scale should be configured together. The three-panel example below shows the optional `fig`/`axs` style for users familiar with Matplotlib; ordinary one-panel examples use `Canvas` directly." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "canvas, (ax1, ax2, ax3) = Canvas.subplots(nrows=1, ncols=3)\n", "ax1.semilogx(x, np.sin(x) + 2)\n", "ax2.semilogy(x, np.exp(x / 4))\n", "ax3.loglog(x, x**2)\n", "ax1.set_title(\"semilogx\")\n", "ax2.set_title(\"semilogy\")\n", "ax3.set_title(\"loglog\")\n", "canvas.show()" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## Axis limits, modes, and autoscaling\n", "\n", "`axis()` accepts Matplotlib-style modes such as `\"equal\"`, `\"tight\"`, and `\"off\"`, or a four-value `[xmin, xmax, ymin, ymax]` limit list. `relim()` and `autoscale_view()` recompute the visible limits from plotted data." ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "canvas = Canvas()\n", "canvas.plot([0, 1, 2], [0, 4, 1])\n", "canvas.axis([0, 2, -1, 5])\n", "canvas.relim()\n", "canvas.autoscale_view(tight=True)\n", "canvas.show()" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Secondary x- and y-axes\n", "\n", "Secondary axes use forward and inverse functions. They are currently rendered by the Matplotlib backend." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "canvas = Canvas()\n", "canvas.plot(x, x**2)\n", "canvas.set_xlabel(\"distance (m)\")\n", "canvas.set_ylabel(\"area (m²)\")\n", "canvas.secondary_xaxis(\n", " \"top\",\n", " functions=(lambda value: value / 1000, lambda value: value * 1000),\n", " label=\"distance (km)\",\n", ")\n", "canvas.secondary_yaxis(\n", " \"right\", functions=(np.sqrt, lambda value: value**2), label=\"length (m)\"\n", ")\n", "canvas.show()" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "## Box aspect and explicit tick labels" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "canvas = Canvas()\n", "canvas.plot([0, 1, 2], [0, 1, 0], marker=\"o\")\n", "canvas.set_box_aspect(1)\n", "canvas.set_xticks([0, 1, 2])\n", "canvas.set_xticklabels([\"start\", \"middle\", \"end\"], rotation=25, color=\"navy\")\n", "canvas.set_yticks([0, 1])\n", "canvas.set_yticklabels([\"low\", \"high\"], fontweight=\"bold\")\n", "canvas.show()" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## Plotly rendering\n", "\n", "The filled polygon, logarithmic scales, axis modes, box aspect, and tick labels also work with Plotly. Secondary axes currently require Matplotlib." ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "plotly_canvas = Canvas()\n", "plotly_canvas.fill(x, np.sin(x) + 2, color=\"purple\", alpha=0.25)\n", "plotly_canvas.loglog(x, x**2, color=\"black\")\n", "plotly_canvas.set_xticklabels([\"small\", \"medium\", \"large\"], color=\"darkgreen\")\n", "plotly_canvas.show(backend=\"plotly\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }