Skip to content

Commit ea4234b

Browse files
committed
import Canvas directly
1 parent 6f9918f commit ea4234b

File tree

8 files changed

+65
-60
lines changed

8 files changed

+65
-60
lines changed

src/maxplotlib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from maxplotlib.canvas.canvas import Canvas
2+
3+
__all__ = ["Canvas"]

src/maxplotlib/canvas/canvas.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ def __init__(self, **kwargs):
3737

3838
# Dictionary to store lines for each subplot
3939
# Key: (row, col), Value: list of lines with their data and kwargs
40-
self.subplots = {}
40+
self._subplots = {}
4141
self._num_subplots = 0
4242

4343
self._subplot_matrix = [[None] * self.ncols for _ in range(self.nrows)]
4444

45+
@property
46+
def subplots(self):
47+
return self._subplots
48+
4549
@property
4650
def layers(self):
4751
layers = []
@@ -88,12 +92,12 @@ def add_tikzfigure(self, **kwargs):
8892

8993
# Store the LinePlot instance by its position for easy access
9094
if label is None:
91-
self.subplots[(row, col)] = tikz_figure
95+
self._subplots[(row, col)] = tikz_figure
9296
else:
93-
self.subplots[label] = tikz_figure
97+
self._subplots[label] = tikz_figure
9498
return tikz_figure
9599

96-
def add_subplot(self, **kwargs):
100+
def add_subplot(self, col: int | None = None, row: int | None = None, label: str | None = None, **kwargs):
97101
"""
98102
Adds a subplot to the figure.
99103
@@ -103,21 +107,19 @@ def add_subplot(self, **kwargs):
103107
- row (int): Row index for the subplot.
104108
- label (str): Label to identify the subplot.
105109
"""
106-
col = kwargs.get("col", None)
107-
row = kwargs.get("row", None)
108-
label = kwargs.get("label", None)
110+
109111

110112
row, col = self.generate_new_rowcol(row, col)
111113

112114
# Initialize the LinePlot for the given subplot position
113-
line_plot = lp.LinePlot(**kwargs)
115+
line_plot = lp.LinePlot(col=col, row=row, label=label, **kwargs)
114116
self._subplot_matrix[row][col] = line_plot
115117

116118
# Store the LinePlot instance by its position for easy access
117119
if label is None:
118-
self.subplots[(row, col)] = line_plot
120+
self._subplots[(row, col)] = line_plot
119121
else:
120-
self.subplots[label] = line_plot
122+
self._subplots[label] = line_plot
121123
return line_plot
122124

123125
def savefig(
@@ -159,19 +161,31 @@ def savefig(
159161
if verbose:
160162
print(f"Saved {full_filepath}")
161163

162-
def plot(self, backend="matplotlib", show=True, savefig=False, layers=None):
164+
def plot(self, backend="matplotlib", savefig=False, layers=None):
165+
if backend == "matplotlib":
166+
return self.plot_matplotlib(savefig=savefig, layers=layers)
167+
elif backend == "plotly":
168+
return self.plot_plotly(savefig=savefig)
169+
else:
170+
raise ValueError("Invalid backend")
171+
172+
def show(self, backend="matplotlib"):
163173
if backend == "matplotlib":
164-
return self.plot_matplotlib(show=show, savefig=savefig, layers=layers)
174+
fig, axs = self.plot(backend="matplotlib", savefig=False, layers=None)
175+
print('hmm')
176+
self._matplotlib_fig.show()
177+
plt.show()
165178
elif backend == "plotly":
166-
self.plot_plotly(show=show, savefig=savefig)
179+
plot = self.plot_plotly(savefig=False)
180+
else:
181+
raise ValueError("Invalid backend")
167182

168-
def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False):
183+
def plot_matplotlib(self, savefig=False, layers=None, usetex=False):
169184
"""
170185
Generate and optionally display the subplots.
171186
172187
Parameters:
173188
filename (str, optional): Filename to save the figure.
174-
show (bool): Whether to display the plot.
175189
"""
176190

177191
tex_fonts = plt_utils.setup_tex_fonts(fontsize=self.fontsize, usetex=usetex)
@@ -205,17 +219,11 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False):
205219

206220
for (row, col), subplot in self.subplots.items():
207221
ax = axes[row][col]
208-
# print(f"{subplot = }")
209222
subplot.plot_matplotlib(ax, layers=layers)
210223
# ax.set_title(f"Subplot ({row}, {col})")
211224
ax.grid()
212-
# Set caption, labels, etc., if needed
213-
# plt.tight_layout()
214225

215-
if show:
216-
plt.show()
217-
# else:
218-
# plt.close()
226+
# Set caption, labels, etc., if needed
219227
self._plotted = True
220228
self._matplotlib_fig = fig
221229
self._matplotlib_axes = axes
@@ -271,8 +279,8 @@ def plot_plotly(self, show=True, savefig=None, usetex=False):
271279
fig.write_image(savefig)
272280

273281
# Show or return the figure
274-
if show:
275-
fig.show()
282+
# if show:
283+
# fig.show()
276284
return fig
277285

278286
# Property getters

tutorials/tutorial_01.ipynb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"metadata": {},
1717
"outputs": [],
1818
"source": [
19-
"import maxplotlib.canvas.canvas as canvas\n",
19+
"from maxplotlib import Canvas\n",
2020
"\n",
2121
"%load_ext autoreload\n",
2222
"%autoreload 2"
@@ -29,14 +29,13 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32-
"c = canvas.Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n",
32+
"c = Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n",
3333
"sp = c.add_subplot(\n",
3434
" grid=True, xlabel=\"(x - 10) * 0.1\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n",
3535
")\n",
3636
"sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n",
3737
"sp.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n",
38-
"c.plot()\n",
39-
"c.savefig(filename=\"tutorial_01_01.pdf\")"
38+
"c.show()"
4039
]
4140
},
4241
{
@@ -46,17 +45,15 @@
4645
"metadata": {},
4746
"outputs": [],
4847
"source": [
49-
"c = canvas.Canvas(width=\"17cm\", ncols=2, nrows=2, ratio=0.5)\n",
48+
"c = Canvas(width=\"17cm\", ncols=2, nrows=2, ratio=0.5)\n",
5049
"sp = c.add_subplot(grid=True)\n",
5150
"c.add_subplot(row=1)\n",
5251
"sp2 = c.add_subplot(row=1, legend=False)\n",
5352
"sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n",
5453
"sp2.add_line(\n",
5554
" [0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\"\n",
5655
")\n",
57-
"c.plot(backend=\"matplotlib\")\n",
58-
"c.plot(backend=\"plotly\")\n",
59-
"c.savefig(filename=\"tutorial_01_02.pdf\")"
56+
"c.show()"
6057
]
6158
},
6259
{
@@ -67,15 +64,13 @@
6764
"outputs": [],
6865
"source": [
6966
"# Test with plotly backend\n",
70-
"c = canvas.Canvas(width=\"17cm\", ratio=0.5)\n",
67+
"c = Canvas(width=\"17cm\", ratio=0.5)\n",
7168
"sp = c.add_subplot(\n",
7269
" grid=True, xlabel=\"x (mm)\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n",
7370
")\n",
7471
"sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\", linestyle=\"-.\")\n",
7572
"sp.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n",
76-
"c.plot(backend=\"matplotlib\")\n",
77-
"c.plot(backend=\"plotly\")\n",
78-
"c.savefig(filename=\"tutorial_01_03.pdf\")"
73+
"c.show()"
7974
]
8075
}
8176
],
@@ -95,7 +90,7 @@
9590
"name": "python",
9691
"nbconvert_exporter": "python",
9792
"pygments_lexer": "ipython3",
98-
"version": "3.13.3"
93+
"version": "3.12.3"
9994
}
10095
},
10196
"nbformat": 4,

tutorials/tutorial_02.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"import maxplotlib.canvas.canvas as canvas\n",
18+
"from maxplotlib import Canvas\n",
1919
"\n",
2020
"%load_ext autoreload\n",
2121
"%autoreload 2"
@@ -28,7 +28,7 @@
2828
"metadata": {},
2929
"outputs": [],
3030
"source": [
31-
"c = canvas.Canvas(width=800, ratio=0.5)\n",
31+
"c = Canvas(width=800, ratio=0.5)\n",
3232
"tikz = c.add_tikzfigure(grid=False)\n",
3333
"\n",
3434
"# Add nodes\n",
@@ -61,7 +61,7 @@
6161
"metadata": {},
6262
"outputs": [],
6363
"source": [
64-
"c = canvas.Canvas(ncols=2, width=\"20cm\", ratio=0.5)\n",
64+
"c = Canvas(ncols=2, width=\"20cm\", ratio=0.5)\n",
6565
"tikz = c.add_tikzfigure(grid=False)\n",
6666
"\n",
6767
"# Add nodes\n",
@@ -111,7 +111,7 @@
111111
"metadata": {},
112112
"outputs": [],
113113
"source": [
114-
"c = canvas.Canvas(width=800, ratio=0.5)\n",
114+
"c = Canvas(width=800, ratio=0.5)\n",
115115
"tikz = c.add_tikzfigure(grid=False)\n",
116116
"\n",
117117
"# Add nodes\n",
@@ -146,7 +146,7 @@
146146
"name": "python",
147147
"nbconvert_exporter": "python",
148148
"pygments_lexer": "ipython3",
149-
"version": "3.13.3"
149+
"version": "3.12.3"
150150
}
151151
},
152152
"nbformat": 4,

tutorials/tutorial_03.ipynb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"import maxplotlib.canvas.canvas as canvas"
18+
"from maxplotlib import Canvas"
1919
]
2020
},
2121
{
@@ -25,16 +25,15 @@
2525
"metadata": {},
2626
"outputs": [],
2727
"source": [
28-
"c = canvas.Canvas(width=\"17cm\", ratio=0.5)\n",
28+
"c = Canvas(width=\"17cm\", ratio=0.5)\n",
2929
"sp = c.add_subplot(\n",
3030
" grid=False, xlabel=\"(x - 10) * 0.1\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n",
3131
")\n",
3232
"sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\", layer=0)\n",
3333
"sp.add_line(\n",
3434
" [0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\", layer=1\n",
3535
")\n",
36-
"# c.plot()\n",
37-
"c.savefig(layer_by_layer=True, filename=\"tutorial_03_01.pdf\")"
36+
"c.show()"
3837
]
3938
}
4039
],
@@ -59,7 +58,7 @@
5958
"name": "python",
6059
"nbconvert_exporter": "python",
6160
"pygments_lexer": "ipython3",
62-
"version": "3.13.3"
61+
"version": "3.12.3"
6362
}
6463
},
6564
"nbformat": 4,

tutorials/tutorial_04.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32-
"import maxplotlib.canvas.canvas as canvas"
32+
"from maxplotlib import Canvas"
3333
]
3434
},
3535
{
@@ -39,7 +39,7 @@
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
42-
"c = canvas.Canvas(width=800, ratio=0.5)\n",
42+
"c = Canvas(width=800, ratio=0.5)\n",
4343
"tikz = c.add_tikzfigure(grid=False)"
4444
]
4545
},
@@ -154,7 +154,7 @@
154154
"name": "python",
155155
"nbconvert_exporter": "python",
156156
"pygments_lexer": "ipython3",
157-
"version": "3.13.3"
157+
"version": "3.12.3"
158158
}
159159
},
160160
"nbformat": 4,

tutorials/tutorial_05.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"source": [
1818
"import numpy as np\n",
1919
"\n",
20-
"import maxplotlib.canvas.canvas as canvas\n",
20+
"from maxplotlib import Canvas\n",
2121
"\n",
2222
"%load_ext autoreload\n",
2323
"%autoreload 2"
@@ -30,7 +30,7 @@
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
33-
"c = canvas.Canvas(width=\"17cm\", ratio=0.5)\n",
33+
"c = Canvas(width=\"17cm\", ratio=0.5)\n",
3434
"sp = c.add_subplot(grid=True, xlabel=\"x\", ylabel=\"y\")\n",
3535
"\n",
3636
"# node_a = sp.add_node(\n",
@@ -49,8 +49,8 @@
4949
"x = np.arange(0, 2 * np.pi, 0.01)\n",
5050
"y = np.sin(x)\n",
5151
"sp.add_line(x, y, label=r\"$\\sin(x)$\")\n",
52-
"# c.plot()\n",
53-
"c.savefig(filename=\"tutorial_05_01.pdf\")"
52+
"\n",
53+
"c.show()"
5454
]
5555
},
5656
{
@@ -78,7 +78,7 @@
7878
"name": "python",
7979
"nbconvert_exporter": "python",
8080
"pygments_lexer": "ipython3",
81-
"version": "3.13.3"
81+
"version": "3.12.3"
8282
}
8383
},
8484
"nbformat": 4,

tutorials/tutorial_06.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"import maxplotlib.canvas.canvas as canvas\n",
18+
"from maxplotlib import Canvas\n",
1919
"import matplotlib.pyplot as plt\n",
2020
"import numpy as np\n",
2121
"\n",
@@ -30,13 +30,13 @@
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
33-
"c = canvas.Canvas(width=\"17cm\", ratio=0.5)\n",
33+
"c = Canvas(width=\"17cm\", ratio=0.5)\n",
3434
"sp = c.add_subplot(grid=False, xlabel=\"x\", ylabel=\"y\")\n",
3535
"# sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\",layer=1)\n",
3636
"data = np.random.random((10, 10))\n",
3737
"sp.add_imshow(data, extent=[1, 10, 1, 20], layer=1)\n",
38-
"# c.plot()\n",
39-
"c.savefig(layer_by_layer=True, filename=\"tutorial_06_01.pdf\")"
38+
"\n",
39+
"c.show()"
4040
]
4141
},
4242
{
@@ -64,7 +64,7 @@
6464
"name": "python",
6565
"nbconvert_exporter": "python",
6666
"pygments_lexer": "ipython3",
67-
"version": "3.13.3"
67+
"version": "3.12.3"
6868
}
6969
},
7070
"nbformat": 4,

0 commit comments

Comments
 (0)