From e1be0c2c6f7ac12639be5941568084255e1807e2 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 26 Jan 2026 15:08:54 +0100 Subject: [PATCH 1/3] bugfix in calculating the figure width --- pyproject.toml | 2 +- src/maxplotlib/backends/matplotlib/utils.py | 26 +++++++-- src/maxplotlib/canvas/canvas.py | 63 +++++++++++++++------ tutorials/tutorial_01.ipynb | 6 +- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 280b999..aa4ecb8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "maxplotlibx" -version = "0.1.2" +version = "0.1.3" description = "A reproducible plotting module with various backends and export options." readme = "README.md" requires-python = ">=3.8" diff --git a/src/maxplotlib/backends/matplotlib/utils.py b/src/maxplotlib/backends/matplotlib/utils.py index 6d9080b..60786b5 100644 --- a/src/maxplotlib/backends/matplotlib/utils.py +++ b/src/maxplotlib/backends/matplotlib/utils.py @@ -56,18 +56,30 @@ def convert_to_inches(length_str): return quantity.to("inch").magnitude # Convert to inches -def _2pt(width, dpi=300): +def _2pt(width, dpi=300, verbose: bool = False): + if verbose: + print(f"Converting width: {width} to points with dpi={dpi}") + if isinstance(width, (int, float)): return width elif isinstance(width, str): length_in = convert_to_inches(width) length_pt = length_in * dpi + if verbose: + print(f"Converted length: {length_in} inches = {length_pt} points") return length_pt else: raise NotImplementedError -def set_size(width, fraction=1, ratio="golden", dpi=300): +# TODO: Use literal types for width and ratio +def set_size( + width: str, + fraction: int | float = 1, + ratio: str | int | float = "golden", + dpi=300, + verbose: bool = False, +) -> tuple: """ Sets figure dimensions to avoid scaling in LaTeX. """ @@ -76,9 +88,11 @@ def set_size(width, fraction=1, ratio="golden", dpi=300): elif width == "beamer": width_pt = 307.28987 else: - width_pt = _2pt(width=width, dpi=dpi) + width_pt = _2pt(width=width, dpi=dpi, verbose=verbose) fig_width_pt = width_pt * fraction + inches_per_pt = 1 / 72.27 + # fig_width_pt = width_pt * fraction # inches_per_pt = 1 / 72.27 # Calculate the figure height based on the desired ratio @@ -91,7 +105,11 @@ def set_size(width, fraction=1, ratio="golden", dpi=300): fig_height_pt = fig_width_pt * ratio else: raise ValueError("Invalid ratio specified.") - fig_dim = (fig_width_pt, fig_height_pt) + + # Convert from points to inches for matplotlib + fig_width_in = fig_width_pt * inches_per_pt + fig_height_in = fig_height_pt * inches_per_pt + fig_dim = (fig_width_in, fig_height_in) return fig_dim diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index c477b3e..4501b85 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -5,6 +5,7 @@ import matplotlib.patches as patches import matplotlib.pyplot as plt from plotly.subplots import make_subplots +from pygame import ver from tikzpics import TikzFigure from maxplotlib.backends.matplotlib.utils import ( @@ -267,9 +268,17 @@ def plot( backend: Backends = "matplotlib", savefig=False, layers=None, + verbose: bool = False, ): + if verbose: + print(f"Plotting figure using backend: {backend}") + if backend == "matplotlib": - return self.plot_matplotlib(savefig=savefig, layers=layers) + return self.plot_matplotlib( + savefig=savefig, + layers=layers, + verbose=verbose, + ) elif backend == "plotly": return self.plot_plotly(savefig=savefig) elif backend == "tikzpics": @@ -280,10 +289,19 @@ def plot( def show( self, backend: Backends = "matplotlib", + verbose: bool = False, ): + if verbose: + print(f"Showing figure using backend: {backend}") + if backend == "matplotlib": - self.plot(backend="matplotlib", savefig=False, layers=None) - self._matplotlib_fig.show() + self.plot( + backend="matplotlib", + savefig=False, + layers=None, + verbose=verbose, + ) + # self._matplotlib_fig.show() elif backend == "plotly": self.plot_plotly(savefig=False) elif backend == "tikzpics": @@ -292,24 +310,34 @@ def show( else: raise ValueError("Invalid backend") - def plot_matplotlib(self, savefig=False, layers=None, usetex=False): + def plot_matplotlib( + self, + savefig: bool = False, + layers: list | None = None, + usetex: bool = False, + verbose: bool = False, + ): """ Generate and optionally display the subplots. Parameters: filename (str, optional): Filename to save the figure. """ - - tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) - - setup_plotstyle( - tex_fonts=tex_fonts, - axes_grid=True, - axes_grid_which="major", - grid_alpha=1.0, - grid_linestyle="dotted", - ) - + if verbose: + print("Generating Matplotlib figure...") + + # tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) + + # setup_plotstyle( + # tex_fonts=tex_fonts, + # axes_grid=True, + # axes_grid_which="major", + # grid_alpha=1.0, + # grid_linestyle="dotted", + # ) + if verbose: + print("Plot style set up.") + print(f"{self._figsize = } {self._width = } {self._ratio = }") if self._figsize is not None: fig_width, fig_height = self._figsize else: @@ -317,7 +345,10 @@ def plot_matplotlib(self, savefig=False, layers=None, usetex=False): width=self._width, ratio=self._ratio, dpi=self.dpi, + verbose=verbose, ) + if verbose: + print(f"Figure size: {fig_width} x {fig_height} points") fig, axes = plt.subplots( self.nrows, @@ -326,7 +357,7 @@ def plot_matplotlib(self, savefig=False, layers=None, usetex=False): squeeze=False, dpi=self.dpi, ) - + return for (row, col), subplot in self.subplots.items(): ax = axes[row][col] if isinstance(subplot, TikzFigure): diff --git a/tutorials/tutorial_01.ipynb b/tutorials/tutorial_01.ipynb index 53d144f..2b110e4 100644 --- a/tutorials/tutorial_01.ipynb +++ b/tutorials/tutorial_01.ipynb @@ -29,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "c = Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n", + "c = Canvas(width=\"17mm\", ratio=0.5, fontsize=12)\n", "c.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n", "c.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n", "c.show()" @@ -94,7 +94,7 @@ ], "metadata": { "kernelspec": { - "display_name": "env_maxplotlib", + "display_name": "env_maxpic", "language": "python", "name": "python3" }, @@ -108,7 +108,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.13.3" } }, "nbformat": 4, From ddb03a194630f39f32215cb40fc68657833b25f1 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 26 Jan 2026 15:21:50 +0100 Subject: [PATCH 2/3] Removed import --- src/maxplotlib/canvas/canvas.py | 19 +++++++++---------- tutorials/tutorial_07_tikzpics.ipynb | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index 4501b85..a64520e 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -5,7 +5,6 @@ import matplotlib.patches as patches import matplotlib.pyplot as plt from plotly.subplots import make_subplots -from pygame import ver from tikzpics import TikzFigure from maxplotlib.backends.matplotlib.utils import ( @@ -326,15 +325,15 @@ def plot_matplotlib( if verbose: print("Generating Matplotlib figure...") - # tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) + tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) - # setup_plotstyle( - # tex_fonts=tex_fonts, - # axes_grid=True, - # axes_grid_which="major", - # grid_alpha=1.0, - # grid_linestyle="dotted", - # ) + setup_plotstyle( + tex_fonts=tex_fonts, + axes_grid=True, + axes_grid_which="major", + grid_alpha=1.0, + grid_linestyle="dotted", + ) if verbose: print("Plot style set up.") print(f"{self._figsize = } {self._width = } {self._ratio = }") @@ -357,7 +356,7 @@ def plot_matplotlib( squeeze=False, dpi=self.dpi, ) - return + for (row, col), subplot in self.subplots.items(): ax = axes[row][col] if isinstance(subplot, TikzFigure): diff --git a/tutorials/tutorial_07_tikzpics.ipynb b/tutorials/tutorial_07_tikzpics.ipynb index d3bc587..b63871a 100644 --- a/tutorials/tutorial_07_tikzpics.ipynb +++ b/tutorials/tutorial_07_tikzpics.ipynb @@ -34,7 +34,7 @@ "\n", "\n", "# TODO: Uncomment if pdflatex is installed\n", - "# c.show(backend=\"tikzpics\")" + "c.show(backend=\"tikzpics\")" ] } ], From 3f90b3df436445f7f706e00bdeae45729c67c5f6 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 26 Jan 2026 15:34:07 +0100 Subject: [PATCH 3/3] Commented out show of tikzpics graph --- tutorials/tutorial_07_tikzpics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/tutorial_07_tikzpics.ipynb b/tutorials/tutorial_07_tikzpics.ipynb index b63871a..d3bc587 100644 --- a/tutorials/tutorial_07_tikzpics.ipynb +++ b/tutorials/tutorial_07_tikzpics.ipynb @@ -34,7 +34,7 @@ "\n", "\n", "# TODO: Uncomment if pdflatex is installed\n", - "c.show(backend=\"tikzpics\")" + "# c.show(backend=\"tikzpics\")" ] } ],