Skip to content

Commit e1be0c2

Browse files
committed
bugfix in calculating the figure width
1 parent 7fb8163 commit e1be0c2

4 files changed

Lines changed: 73 additions & 24 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: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import matplotlib.patches as patches
66
import matplotlib.pyplot as plt
77
from plotly.subplots import make_subplots
8+
from pygame import ver
89
from tikzpics import TikzFigure
910

1011
from maxplotlib.backends.matplotlib.utils import (
@@ -267,9 +268,17 @@ def plot(
267268
backend: Backends = "matplotlib",
268269
savefig=False,
269270
layers=None,
271+
verbose: bool = False,
270272
):
273+
if verbose:
274+
print(f"Plotting figure using backend: {backend}")
275+
271276
if backend == "matplotlib":
272-
return self.plot_matplotlib(savefig=savefig, layers=layers)
277+
return self.plot_matplotlib(
278+
savefig=savefig,
279+
layers=layers,
280+
verbose=verbose,
281+
)
273282
elif backend == "plotly":
274283
return self.plot_plotly(savefig=savefig)
275284
elif backend == "tikzpics":
@@ -280,10 +289,19 @@ def plot(
280289
def show(
281290
self,
282291
backend: Backends = "matplotlib",
292+
verbose: bool = False,
283293
):
294+
if verbose:
295+
print(f"Showing figure using backend: {backend}")
296+
284297
if backend == "matplotlib":
285-
self.plot(backend="matplotlib", savefig=False, layers=None)
286-
self._matplotlib_fig.show()
298+
self.plot(
299+
backend="matplotlib",
300+
savefig=False,
301+
layers=None,
302+
verbose=verbose,
303+
)
304+
# self._matplotlib_fig.show()
287305
elif backend == "plotly":
288306
self.plot_plotly(savefig=False)
289307
elif backend == "tikzpics":
@@ -292,32 +310,45 @@ def show(
292310
else:
293311
raise ValueError("Invalid backend")
294312

295-
def plot_matplotlib(self, savefig=False, layers=None, usetex=False):
313+
def plot_matplotlib(
314+
self,
315+
savefig: bool = False,
316+
layers: list | None = None,
317+
usetex: bool = False,
318+
verbose: bool = False,
319+
):
296320
"""
297321
Generate and optionally display the subplots.
298322
299323
Parameters:
300324
filename (str, optional): Filename to save the figure.
301325
"""
302-
303-
tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex)
304-
305-
setup_plotstyle(
306-
tex_fonts=tex_fonts,
307-
axes_grid=True,
308-
axes_grid_which="major",
309-
grid_alpha=1.0,
310-
grid_linestyle="dotted",
311-
)
312-
326+
if verbose:
327+
print("Generating Matplotlib figure...")
328+
329+
# tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex)
330+
331+
# setup_plotstyle(
332+
# tex_fonts=tex_fonts,
333+
# axes_grid=True,
334+
# axes_grid_which="major",
335+
# grid_alpha=1.0,
336+
# grid_linestyle="dotted",
337+
# )
338+
if verbose:
339+
print("Plot style set up.")
340+
print(f"{self._figsize = } {self._width = } {self._ratio = }")
313341
if self._figsize is not None:
314342
fig_width, fig_height = self._figsize
315343
else:
316344
fig_width, fig_height = set_size(
317345
width=self._width,
318346
ratio=self._ratio,
319347
dpi=self.dpi,
348+
verbose=verbose,
320349
)
350+
if verbose:
351+
print(f"Figure size: {fig_width} x {fig_height} points")
321352

322353
fig, axes = plt.subplots(
323354
self.nrows,
@@ -326,7 +357,7 @@ def plot_matplotlib(self, savefig=False, layers=None, usetex=False):
326357
squeeze=False,
327358
dpi=self.dpi,
328359
)
329-
360+
return
330361
for (row, col), subplot in self.subplots.items():
331362
ax = axes[row][col]
332363
if isinstance(subplot, TikzFigure):

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)