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..a64520e 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -267,9 +267,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 +288,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,13 +309,21 @@ 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. """ + if verbose: + print("Generating Matplotlib figure...") tex_fonts = setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) @@ -309,7 +334,9 @@ def plot_matplotlib(self, savefig=False, layers=None, usetex=False): 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 +344,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, 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,