Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/maxplotlib/backends/matplotlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/maxplotlib/backends/matplotlib/utils_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]

Expand Down
29 changes: 20 additions & 9 deletions src/maxplotlib/canvas/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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}")

Expand All @@ -158,16 +166,15 @@ 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.

Parameters:
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,
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down
Loading