From e99e6dcd1eaebfbab836399db726b75cc8711b3a Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 26 Apr 2025 10:31:04 +0200 Subject: [PATCH 1/4] Set usetex=False by default --- src/maxplotlib/backends/matplotlib/utils.py | 4 ++-- src/maxplotlib/canvas/canvas.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/maxplotlib/backends/matplotlib/utils.py b/src/maxplotlib/backends/matplotlib/utils.py index f25c7c7..6e15ca5 100644 --- a/src/maxplotlib/backends/matplotlib/utils.py +++ b/src/maxplotlib/backends/matplotlib/utils.py @@ -15,12 +15,12 @@ from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection -def setup_tex_fonts(fontsize=14): +def setup_tex_fonts(fontsize=14, usetex=False): """ Sets up LaTeX fonts for plotting. """ tex_fonts = { - "text.usetex": True, + "text.usetex": usetex, "font.family": "serif", "pgf.rcfonts": False, "axes.labelsize": fontsize, diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index 072c665..f7b9162 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -158,7 +158,7 @@ def plot(self, backend="matplotlib", show=True, savefig=False, layers=None): elif backend == "plotly": self.plot_plotly(show=show, savefig=savefig) - def plot_matplotlib(self, show=True, savefig=False, layers=None): + def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False): """ Generate and optionally display the subplots. @@ -167,7 +167,7 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None): show (bool): Whether to display the plot. """ - tex_fonts = plt_utils.setup_tex_fonts(fontsize=self.fontsize) + tex_fonts = plt_utils.setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) plt_utils.setup_plotstyle( tex_fonts=tex_fonts, @@ -210,7 +210,7 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None): # plt.close() return fig, axes - def plot_plotly(self, show=True, savefig=None): + def plot_plotly(self, show=True, savefig=None, usetex=False): """ Generate and optionally display the subplots using Plotly. @@ -220,7 +220,8 @@ def plot_plotly(self, show=True, savefig=None): """ tex_fonts = plt_utils.setup_tex_fonts( - fontsize=self.fontsize + fontsize=self.fontsize, + usetex=usetex, ) # adjust or redefine for Plotly if needed # Set default width and height if not specified From 1b50734d59463df41ba9d8d23239a74a8d270a92 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 1 May 2025 12:14:18 +0200 Subject: [PATCH 2/4] Don't plot if plotted --- .../backends/matplotlib/utils_old.py | 4 ++-- src/maxplotlib/canvas/canvas.py | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/maxplotlib/backends/matplotlib/utils_old.py b/src/maxplotlib/backends/matplotlib/utils_old.py index 766ca33..25abfdc 100644 --- a/src/maxplotlib/backends/matplotlib/utils_old.py +++ b/src/maxplotlib/backends/matplotlib/utils_old.py @@ -419,7 +419,7 @@ def scale_axis( if not axs_in == None: ax = axs_in - print("precision", precision, precision, precision) + # print("precision", precision, precision, precision) plt.sca(ax) if axis == "x": if locs_labels == None: @@ -520,7 +520,7 @@ def add_figure_label( ax=None, ): limits = self.get_limits(ax) - print(limits) + # print(limits) lx = limits[1] - limits[0] ly = limits[3] - limits[2] diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index f7b9162..94b3de7 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -33,6 +33,7 @@ def __init__(self, **kwargs): self._width = kwargs.get("width", "17cm") self._ratio = kwargs.get("ratio", "golden") self._gridspec_kw = kwargs.get("gridspec_kw", {"wspace": 0.08, "hspace": 0.1}) + self._plotted = False # Dictionary to store lines for each subplot # Key: (row, col), Value: list of lines with their data and kwargs @@ -126,6 +127,7 @@ def savefig( layers=None, layer_by_layer=False, verbose=False, + plot=True, ): filename_no_extension, extension = os.path.splitext(filename) if backend == "matplotlib": @@ -145,10 +147,16 @@ def savefig( full_filepath = filename else: full_filepath = f"{filename_no_extension}_{layers}.{extension}" - fig, axs = self.plot( - show=False, backend="matplotlib", savefig=True, layers=layers - ) - fig.savefig(full_filepath) + print(f"Save to {full_filepath}") + if self._plotted: + self._matplotlib_fig.savefig(full_filepath) + else: + + fig, axs = self.plot( + show=False, backend="matplotlib", savefig=True, layers=layers + ) + print('done plotting') + fig.savefig(full_filepath) if verbose: print(f"Saved {full_filepath}") @@ -166,7 +174,7 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False): filename (str, optional): Filename to save the figure. show (bool): Whether to display the plot. """ - + print(f"Plotting with maxplotlib ") tex_fonts = plt_utils.setup_tex_fonts(fontsize=self.fontsize, usetex=usetex) plt_utils.setup_plotstyle( @@ -198,6 +206,7 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False): for (row, col), subplot in self.subplots.items(): ax = axes[row][col] + print(f"subplot.plot_matplotlib(ax, layers=layers)") subplot.plot_matplotlib(ax, layers=layers) # ax.set_title(f"Subplot ({row}, {col})") ax.grid() @@ -208,6 +217,9 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False): plt.show() # else: # plt.close() + self._plotted = True + self._matplotlib_fig = fig + self._matplotlib_axes = axes return fig, axes def plot_plotly(self, show=True, savefig=None, usetex=False): From 6bce489f6444e202bd67922883318f4f6bc4b2e8 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 21 Jun 2025 22:32:33 +0200 Subject: [PATCH 3/4] formatting --- src/maxplotlib/canvas/canvas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index 901c0fe..1ce06a4 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -151,11 +151,11 @@ def savefig( if self._plotted: self._matplotlib_fig.savefig(full_filepath) else: - + fig, axs = self.plot( show=False, backend="matplotlib", savefig=True, layers=layers ) - print('done plotting') + print("done plotting") fig.savefig(full_filepath) if verbose: print(f"Saved {full_filepath}") From b2ddb93208100aa0956cb53f048694754428b94a Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 22 Jun 2025 09:51:45 +0200 Subject: [PATCH 4/4] cleanup --- src/maxplotlib/canvas/canvas.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index 1ce06a4..063ea82 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -155,7 +155,6 @@ def savefig( fig, axs = self.plot( show=False, backend="matplotlib", savefig=True, layers=layers ) - print("done plotting") fig.savefig(full_filepath) if verbose: print(f"Saved {full_filepath}") @@ -206,7 +205,6 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None, usetex=False): for (row, col), subplot in self.subplots.items(): ax = axes[row][col] - print(f"subplot.plot_matplotlib(ax, layers=layers)") subplot.plot_matplotlib(ax, layers=layers) # ax.set_title(f"Subplot ({row}, {col})") ax.grid()