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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 22 additions & 4 deletions src/maxplotlib/backends/matplotlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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
Expand All @@ -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


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

Expand All @@ -309,15 +334,20 @@ 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:
fig_width, fig_height = set_size(
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,
Expand Down
6 changes: 3 additions & 3 deletions tutorials/tutorial_01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down Expand Up @@ -94,7 +94,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "env_maxplotlib",
"display_name": "env_maxpic",
"language": "python",
"name": "python3"
},
Expand All @@ -108,7 +108,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down
Loading