|
5 | 5 | "id": "0", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
| 8 | + "# Tutorial 01 — Quick Start\n", |
8 | 9 | "\n", |
9 | | - "# Tutorial 1\n" |
| 10 | + "**maxplotlib** is a thin, expressive wrapper around Matplotlib that simplifies\n", |
| 11 | + "creating publication-quality figures. Its central class, `Canvas`, replaces the\n", |
| 12 | + "usual `fig, ax = plt.subplots()` boilerplate and exposes a clean, chainable API.\n", |
| 13 | + "\n", |
| 14 | + "This notebook walks you through the very basics:\n", |
| 15 | + "- creating a canvas and a subplot\n", |
| 16 | + "- adding lines\n", |
| 17 | + "- using the canvas-level shortcut API\n", |
| 18 | + "- saving figures" |
10 | 19 | ] |
11 | 20 | }, |
12 | 21 | { |
|
17 | 26 | "outputs": [], |
18 | 27 | "source": [ |
19 | 28 | "from maxplotlib import Canvas\n", |
| 29 | + "import numpy as np\n", |
| 30 | + "\n", |
| 31 | + "%matplotlib inline" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "id": "2", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "## 1 Minimal example\n", |
20 | 40 | "\n", |
21 | | - "%load_ext autoreload\n", |
22 | | - "%autoreload 2" |
| 41 | + "`Canvas.subplots()` mirrors `plt.subplots()`. It returns the canvas and one (or\n", |
| 42 | + "more) subplot axes objects." |
23 | 43 | ] |
24 | 44 | }, |
25 | 45 | { |
26 | 46 | "cell_type": "code", |
27 | 47 | "execution_count": null, |
28 | | - "id": "2", |
| 48 | + "id": "3", |
29 | 49 | "metadata": {}, |
30 | 50 | "outputs": [], |
31 | 51 | "source": [ |
32 | | - "c = Canvas(ratio=0.5, fontsize=12)\n", |
33 | | - "c.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n", |
34 | | - "c.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n", |
35 | | - "c.show()" |
| 52 | + "x = np.linspace(0, 2 * np.pi, 200)\n", |
| 53 | + "y = np.sin(x)\n", |
| 54 | + "\n", |
| 55 | + "canvas, ax = Canvas.subplots()\n", |
| 56 | + "ax.plot(x, y)\n", |
| 57 | + "canvas.show()" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "id": "4", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "## 2 Multiple lines with labels, colors, and linestyles" |
36 | 66 | ] |
37 | 67 | }, |
38 | 68 | { |
39 | 69 | "cell_type": "code", |
40 | 70 | "execution_count": null, |
41 | | - "id": "3", |
| 71 | + "id": "5", |
42 | 72 | "metadata": {}, |
43 | 73 | "outputs": [], |
44 | 74 | "source": [ |
45 | | - "# You can also explicitly create a subplot and add lines to it\n", |
| 75 | + "canvas, ax = Canvas.subplots()\n", |
46 | 76 | "\n", |
47 | | - "c = Canvas(ratio=0.5, fontsize=12)\n", |
48 | | - "sp = c.add_subplot(\n", |
49 | | - " grid=True, xlabel=\"(x - 10) * 0.1\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n", |
50 | | - ")\n", |
| 77 | + "ax.plot(x, np.sin(x), label='sin(x)', color='royalblue', linestyle='solid', linewidth=2)\n", |
| 78 | + "ax.plot(x, np.cos(x), label='cos(x)', color='tomato', linestyle='dashed', linewidth=2)\n", |
| 79 | + "ax.plot(x, np.sin(2*x), label='sin(2x)', color='forestgreen',linestyle='dotted', linewidth=1.5)\n", |
| 80 | + "\n", |
| 81 | + "ax.set_xlabel('x')\n", |
| 82 | + "ax.set_ylabel('y')\n", |
| 83 | + "ax.set_title('Sine and Cosine')\n", |
| 84 | + "ax.set_legend(True)\n", |
51 | 85 | "\n", |
52 | | - "sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n", |
53 | | - "sp.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n", |
54 | | - "c.show()" |
| 86 | + "canvas.show()" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "6", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "## 3 Canvas-level shortcut API\n", |
| 95 | + "\n", |
| 96 | + "For single-subplot figures you can call plot methods directly on the `Canvas`\n", |
| 97 | + "object — they are forwarded to the subplot at `row=0, col=0`." |
55 | 98 | ] |
56 | 99 | }, |
57 | 100 | { |
58 | 101 | "cell_type": "code", |
59 | 102 | "execution_count": null, |
60 | | - "id": "4", |
| 103 | + "id": "7", |
61 | 104 | "metadata": {}, |
62 | 105 | "outputs": [], |
63 | 106 | "source": [ |
64 | | - "# Example with multiple subplots\n", |
65 | | - "\n", |
66 | | - "c = Canvas(width=\"10cm\", ncols=2, nrows=2, ratio=0.5)\n", |
67 | | - "sp = c.add_subplot(grid=True)\n", |
68 | | - "c.add_subplot(row=1)\n", |
69 | | - "sp2 = c.add_subplot(row=1, legend=False)\n", |
70 | | - "sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n", |
71 | | - "sp2.add_line(\n", |
72 | | - " [0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\"\n", |
73 | | - ")\n", |
74 | | - "c.show()" |
| 107 | + "canvas = Canvas(ratio=0.5, fontsize=12)\n", |
| 108 | + "\n", |
| 109 | + "canvas.add_line(x, np.sin(x), label='sin(x)', color='steelblue')\n", |
| 110 | + "canvas.add_line(x, np.cos(x), label='cos(x)', color='darkorange', linestyle='dashed')\n", |
| 111 | + "\n", |
| 112 | + "canvas.set_xlabel('angle (rad)')\n", |
| 113 | + "canvas.set_ylabel('amplitude')\n", |
| 114 | + "canvas.set_title('Using canvas-level methods')\n", |
| 115 | + "canvas.set_legend(True)\n", |
| 116 | + "canvas.set_grid(True)\n", |
| 117 | + "\n", |
| 118 | + "canvas.show()" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "id": "8", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "## 4 Configuring the subplot at creation time\n", |
| 127 | + "\n", |
| 128 | + "`add_subplot()` accepts convenience kwargs so you can set labels, grid, and\n", |
| 129 | + "legend in one call." |
75 | 130 | ] |
76 | 131 | }, |
77 | 132 | { |
78 | 133 | "cell_type": "code", |
79 | 134 | "execution_count": null, |
80 | | - "id": "5", |
| 135 | + "id": "9", |
81 | 136 | "metadata": {}, |
82 | 137 | "outputs": [], |
83 | 138 | "source": [ |
84 | | - "# Test with plotly backend\n", |
85 | | - "c = Canvas(ratio=0.5)\n", |
86 | | - "sp = c.add_subplot(\n", |
87 | | - " grid=True, xlabel=\"x (mm)\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n", |
| 139 | + "canvas = Canvas(ratio=0.5)\n", |
| 140 | + "ax = canvas.add_subplot(\n", |
| 141 | + " title='Configured at creation',\n", |
| 142 | + " xlabel='x',\n", |
| 143 | + " ylabel='f(x)',\n", |
| 144 | + " grid=True,\n", |
| 145 | + " legend=True,\n", |
88 | 146 | ")\n", |
89 | | - "sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\", linestyle=\"-.\")\n", |
90 | | - "sp.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n", |
91 | | - "c.show()" |
| 147 | + "\n", |
| 148 | + "ax.plot(x, np.sin(x), label='sin', color='royalblue')\n", |
| 149 | + "ax.plot(x, x / (2*np.pi), label='x/2π', color='coral', linestyle='dashed')\n", |
| 150 | + "\n", |
| 151 | + "canvas.show()" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "markdown", |
| 156 | + "id": "10", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "## 5 Saving a figure\n", |
| 160 | + "\n", |
| 161 | + "Use `canvas.savefig()` to write the figure to disk. The file extension\n", |
| 162 | + "determines the format; pass `backend='matplotlib'` for PDF output." |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "id": "11", |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [], |
| 171 | + "source": [ |
| 172 | + "canvas = Canvas(ratio=0.5)\n", |
| 173 | + "ax = canvas.add_subplot(xlabel='x', ylabel='sin(x)', grid=True)\n", |
| 174 | + "ax.plot(x, np.sin(x), color='steelblue')\n", |
| 175 | + "\n", |
| 176 | + "canvas.savefig('tutorial_01_output.png')\n", |
| 177 | + "print('Figure saved to tutorial_01_output.png')" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "id": "12", |
| 183 | + "metadata": {}, |
| 184 | + "source": [ |
| 185 | + "## Summary\n", |
| 186 | + "\n", |
| 187 | + "| Task | Code |\n", |
| 188 | + "|---|---|\n", |
| 189 | + "| Create canvas + subplot | `canvas, ax = Canvas.subplots()` |\n", |
| 190 | + "| Add a line | `ax.plot(x, y, label=..., color=..., linestyle=...)` |\n", |
| 191 | + "| Canvas shortcut | `canvas.add_line(x, y, ...)` |\n", |
| 192 | + "| Labels / title | `ax.set_xlabel()`, `ax.set_ylabel()`, `ax.set_title()` |\n", |
| 193 | + "| Legend / grid | `ax.set_legend(True)`, `ax.set_grid(True)` |\n", |
| 194 | + "| Display | `canvas.show()` |\n", |
| 195 | + "| Save | `canvas.savefig('out.png')` |\n", |
| 196 | + "\n", |
| 197 | + "Continue to **Tutorial 02** to learn about multi-subplot layouts." |
92 | 198 | ] |
93 | 199 | } |
94 | 200 | ], |
95 | 201 | "metadata": { |
96 | 202 | "kernelspec": { |
97 | | - "display_name": "env_maxpic", |
| 203 | + "display_name": "Python 3", |
98 | 204 | "language": "python", |
99 | 205 | "name": "python3" |
100 | 206 | }, |
101 | 207 | "language_info": { |
102 | | - "codemirror_mode": { |
103 | | - "name": "ipython", |
104 | | - "version": 3 |
105 | | - }, |
106 | | - "file_extension": ".py", |
107 | | - "mimetype": "text/x-python", |
108 | 208 | "name": "python", |
109 | | - "nbconvert_exporter": "python", |
110 | | - "pygments_lexer": "ipython3", |
111 | | - "version": "3.13.3" |
| 209 | + "version": "3.10.0" |
112 | 210 | } |
113 | 211 | }, |
114 | 212 | "nbformat": 4, |
|
0 commit comments