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/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 072c665..2e24d3f 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}") @@ -158,7 +166,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. @@ -166,8 +174,7 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None): filename (str, optional): Filename to save the figure. 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, @@ -208,9 +215,12 @@ def plot_matplotlib(self, show=True, savefig=False, layers=None): 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): + def plot_plotly(self, show=True, savefig=None, usetex=False): """ Generate and optionally display the subplots using Plotly. @@ -220,7 +230,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