Skip to content

Commit 8f39b69

Browse files
committed
Updated the tutorials
1 parent bf6f12f commit 8f39b69

8 files changed

Lines changed: 2419 additions & 245 deletions

tutorials/tutorial_01.ipynb

Lines changed: 145 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
"id": "0",
66
"metadata": {},
77
"source": [
8+
"# Tutorial 01 — Quick Start\n",
89
"\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"
1019
]
1120
},
1221
{
@@ -17,98 +26,187 @@
1726
"outputs": [],
1827
"source": [
1928
"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",
2040
"\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."
2343
]
2444
},
2545
{
2646
"cell_type": "code",
2747
"execution_count": null,
28-
"id": "2",
48+
"id": "3",
2949
"metadata": {},
3050
"outputs": [],
3151
"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"
3666
]
3767
},
3868
{
3969
"cell_type": "code",
4070
"execution_count": null,
41-
"id": "3",
71+
"id": "5",
4272
"metadata": {},
4373
"outputs": [],
4474
"source": [
45-
"# You can also explicitly create a subplot and add lines to it\n",
75+
"canvas, ax = Canvas.subplots()\n",
4676
"\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",
5185
"\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`."
5598
]
5699
},
57100
{
58101
"cell_type": "code",
59102
"execution_count": null,
60-
"id": "4",
103+
"id": "7",
61104
"metadata": {},
62105
"outputs": [],
63106
"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."
75130
]
76131
},
77132
{
78133
"cell_type": "code",
79134
"execution_count": null,
80-
"id": "5",
135+
"id": "9",
81136
"metadata": {},
82137
"outputs": [],
83138
"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",
88146
")\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."
92198
]
93199
}
94200
],
95201
"metadata": {
96202
"kernelspec": {
97-
"display_name": "env_maxpic",
203+
"display_name": "Python 3",
98204
"language": "python",
99205
"name": "python3"
100206
},
101207
"language_info": {
102-
"codemirror_mode": {
103-
"name": "ipython",
104-
"version": 3
105-
},
106-
"file_extension": ".py",
107-
"mimetype": "text/x-python",
108208
"name": "python",
109-
"nbconvert_exporter": "python",
110-
"pygments_lexer": "ipython3",
111-
"version": "3.13.3"
209+
"version": "3.10.0"
112210
}
113211
},
114212
"nbformat": 4,

0 commit comments

Comments
 (0)