Skip to content
Closed
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
3 changes: 2 additions & 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.1"
version = "0.1.3"
description = "A reproducible plotting module with various backends and export options."
readme = "README.md"
requires-python = ">=3.8"
Expand All @@ -18,6 +18,7 @@ dependencies = [
"matplotlib",
"pint",
"plotly",
"tikzpics>=0.1.1",
]
[project.optional-dependencies]
test = [
Expand Down
36 changes: 22 additions & 14 deletions src/maxplotlib/backends/matplotlib/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
# import sys; from os.path import dirname; sys.path.append(f'{dirname(__file__)}/../../')

# import matplotlib.pylab as pylab
import math
import pickle
from pathlib import Path

import _pickle as cPickle
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import pint
from matplotlib.collections import PatchCollection
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection


def setup_tex_fonts(fontsize=14, usetex=False):
Expand Down Expand Up @@ -65,19 +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
# print(f"{length_in = } {length_pt = }")
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 @@ -86,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 @@ -101,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
Loading
Loading