Skip to content

Commit e94e3d9

Browse files
committed
Merge branch 'devel' into version-0-1-3
2 parents 608333b + 5dca4c9 commit e94e3d9

4 files changed

Lines changed: 61 additions & 13 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "maxplotlibx"
7-
version = "0.1.2"
7+
version = "0.1.3"
88
description = "A reproducible plotting module with various backends and export options."
99
readme = "README.md"
1010
requires-python = ">=3.8"

src/maxplotlib/backends/matplotlib/utils.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,30 @@ def convert_to_inches(length_str):
5656
return quantity.to("inch").magnitude # Convert to inches
5757

5858

59-
def _2pt(width, dpi=300):
59+
def _2pt(width, dpi=300, verbose: bool = False):
60+
if verbose:
61+
print(f"Converting width: {width} to points with dpi={dpi}")
62+
6063
if isinstance(width, (int, float)):
6164
return width
6265
elif isinstance(width, str):
6366
length_in = convert_to_inches(width)
6467
length_pt = length_in * dpi
68+
if verbose:
69+
print(f"Converted length: {length_in} inches = {length_pt} points")
6570
return length_pt
6671
else:
6772
raise NotImplementedError
6873

6974

70-
def set_size(width, fraction=1, ratio="golden", dpi=300):
75+
# TODO: Use literal types for width and ratio
76+
def set_size(
77+
width: str,
78+
fraction: int | float = 1,
79+
ratio: str | int | float = "golden",
80+
dpi=300,
81+
verbose: bool = False,
82+
) -> tuple:
7183
"""
7284
Sets figure dimensions to avoid scaling in LaTeX.
7385
"""
@@ -76,9 +88,11 @@ def set_size(width, fraction=1, ratio="golden", dpi=300):
7688
elif width == "beamer":
7789
width_pt = 307.28987
7890
else:
79-
width_pt = _2pt(width=width, dpi=dpi)
91+
width_pt = _2pt(width=width, dpi=dpi, verbose=verbose)
8092

8193
fig_width_pt = width_pt * fraction
94+
inches_per_pt = 1 / 72.27
95+
# fig_width_pt = width_pt * fraction
8296
# inches_per_pt = 1 / 72.27
8397

8498
# Calculate the figure height based on the desired ratio
@@ -91,7 +105,11 @@ def set_size(width, fraction=1, ratio="golden", dpi=300):
91105
fig_height_pt = fig_width_pt * ratio
92106
else:
93107
raise ValueError("Invalid ratio specified.")
94-
fig_dim = (fig_width_pt, fig_height_pt)
108+
109+
# Convert from points to inches for matplotlib
110+
fig_width_in = fig_width_pt * inches_per_pt
111+
fig_height_in = fig_height_pt * inches_per_pt
112+
fig_dim = (fig_width_in, fig_height_in)
95113
return fig_dim
96114

97115

src/maxplotlib/canvas/canvas.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,17 @@ def plot(
267267
backend: Backends = "matplotlib",
268268
savefig=False,
269269
layers=None,
270+
verbose: bool = False,
270271
):
272+
if verbose:
273+
print(f"Plotting figure using backend: {backend}")
274+
271275
if backend == "matplotlib":
272-
return self.plot_matplotlib(savefig=savefig, layers=layers)
276+
return self.plot_matplotlib(
277+
savefig=savefig,
278+
layers=layers,
279+
verbose=verbose,
280+
)
273281
elif backend == "plotly":
274282
return self.plot_plotly(savefig=savefig)
275283
elif backend == "tikzpics":
@@ -280,10 +288,19 @@ def plot(
280288
def show(
281289
self,
282290
backend: Backends = "matplotlib",
291+
verbose: bool = False,
283292
):
293+
if verbose:
294+
print(f"Showing figure using backend: {backend}")
295+
284296
if backend == "matplotlib":
285-
self.plot(backend="matplotlib", savefig=False, layers=None)
286-
self._matplotlib_fig.show()
297+
self.plot(
298+
backend="matplotlib",
299+
savefig=False,
300+
layers=None,
301+
verbose=verbose,
302+
)
303+
# self._matplotlib_fig.show()
287304
elif backend == "plotly":
288305
self.plot_plotly(savefig=False)
289306
elif backend == "tikzpics":
@@ -292,13 +309,21 @@ def show(
292309
else:
293310
raise ValueError("Invalid backend")
294311

295-
def plot_matplotlib(self, savefig=False, layers=None, usetex=False):
312+
def plot_matplotlib(
313+
self,
314+
savefig: bool = False,
315+
layers: list | None = None,
316+
usetex: bool = False,
317+
verbose: bool = False,
318+
):
296319
"""
297320
Generate and optionally display the subplots.
298321
299322
Parameters:
300323
filename (str, optional): Filename to save the figure.
301324
"""
325+
if verbose:
326+
print("Generating Matplotlib figure...")
302327

303328
tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex)
304329

@@ -309,15 +334,20 @@ def plot_matplotlib(self, savefig=False, layers=None, usetex=False):
309334
grid_alpha=1.0,
310335
grid_linestyle="dotted",
311336
)
312-
337+
if verbose:
338+
print("Plot style set up.")
339+
print(f"{self._figsize = } {self._width = } {self._ratio = }")
313340
if self._figsize is not None:
314341
fig_width, fig_height = self._figsize
315342
else:
316343
fig_width, fig_height = set_size(
317344
width=self._width,
318345
ratio=self._ratio,
319346
dpi=self.dpi,
347+
verbose=verbose,
320348
)
349+
if verbose:
350+
print(f"Figure size: {fig_width} x {fig_height} points")
321351

322352
fig, axes = plt.subplots(
323353
self.nrows,

tutorials/tutorial_01.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-
"c = Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n",
32+
"c = Canvas(width=\"17mm\", ratio=0.5, fontsize=12)\n",
3333
"c.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n",
3434
"c.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n",
3535
"c.show()"
@@ -94,7 +94,7 @@
9494
],
9595
"metadata": {
9696
"kernelspec": {
97-
"display_name": "env_maxplotlib",
97+
"display_name": "env_maxpic",
9898
"language": "python",
9999
"name": "python3"
100100
},
@@ -108,7 +108,7 @@
108108
"name": "python",
109109
"nbconvert_exporter": "python",
110110
"pygments_lexer": "ipython3",
111-
"version": "3.12.3"
111+
"version": "3.13.3"
112112
}
113113
},
114114
"nbformat": 4,

0 commit comments

Comments
 (0)