maxplotlibx 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
maxplotlib/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from maxplotlib.canvas.canvas import Canvas
2
+
3
+ __all__ = ["Canvas"]
@@ -0,0 +1,136 @@
1
+ # import sys; from os.path import dirname; sys.path.append(f'{dirname(__file__)}/../../')
2
+
3
+ # import matplotlib.pylab as pylab
4
+ import math
5
+ import pickle
6
+ from pathlib import Path
7
+
8
+ import _pickle as cPickle
9
+ import matplotlib.colors as mcolors
10
+ import matplotlib.pyplot as plt
11
+ import numpy as np
12
+ import pint
13
+ from matplotlib.collections import PatchCollection
14
+ from mpl_toolkits.mplot3d import Axes3D
15
+ from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection
16
+
17
+
18
+ def setup_tex_fonts(fontsize=14, usetex=False):
19
+ """
20
+ Sets up LaTeX fonts for plotting.
21
+ """
22
+ tex_fonts = {
23
+ "text.usetex": usetex,
24
+ "font.family": "serif",
25
+ "pgf.rcfonts": False,
26
+ "axes.labelsize": fontsize,
27
+ "font.size": fontsize,
28
+ "legend.fontsize": fontsize,
29
+ "xtick.labelsize": fontsize,
30
+ "ytick.labelsize": fontsize,
31
+ }
32
+ plt.rcParams.update(tex_fonts)
33
+ return tex_fonts
34
+
35
+
36
+ def setup_plotstyle(
37
+ tex_fonts=None,
38
+ axes_grid=False,
39
+ axes_grid_which="major",
40
+ grid_alpha=1.0,
41
+ grid_linestyle="dotted",
42
+ ):
43
+ """
44
+ Configures the plot style.
45
+ """
46
+ if tex_fonts:
47
+ plt.rcParams.update(tex_fonts)
48
+ plt.rcParams["axes.grid"] = axes_grid
49
+ plt.rcParams["axes.grid.which"] = axes_grid_which
50
+ plt.rcParams["grid.alpha"] = grid_alpha
51
+ plt.rcParams["grid.linestyle"] = grid_linestyle
52
+ plt.rcParams["xtick.direction"] = "in"
53
+ plt.rcParams["ytick.direction"] = "in"
54
+ plt.rcParams["xtick.major.pad"] = 8
55
+ plt.rcParams["ytick.major.pad"] = 8
56
+
57
+
58
+ # TODO: Use the other unit package
59
+ # Create a UnitRegistry
60
+ ureg = pint.UnitRegistry()
61
+
62
+
63
+ def convert_to_inches(length_str):
64
+ quantity = ureg(length_str) # Parse the input string
65
+ return quantity.to("inch").magnitude # Convert to inches
66
+
67
+
68
+ def _2pt(width, dpi=300):
69
+ if isinstance(width, (int, float)):
70
+ return width
71
+ elif isinstance(width, str):
72
+ length_in = convert_to_inches(width)
73
+ length_pt = length_in * dpi
74
+ # print(f"{length_in = } {length_pt = }")
75
+ return length_pt
76
+ else:
77
+ raise NotImplementedError
78
+
79
+
80
+ def set_size(width, fraction=1, ratio="golden", dpi=300):
81
+ """
82
+ Sets figure dimensions to avoid scaling in LaTeX.
83
+ """
84
+ if width == "thesis":
85
+ width_pt = 426.79135
86
+ elif width == "beamer":
87
+ width_pt = 307.28987
88
+ else:
89
+ width_pt = _2pt(width=width, dpi=dpi)
90
+
91
+ fig_width_pt = width_pt * fraction
92
+ # inches_per_pt = 1 / 72.27
93
+
94
+ # Calculate the figure height based on the desired ratio
95
+ if ratio == "golden":
96
+ golden_ratio = (5**0.5 - 1) / 2
97
+ fig_height_pt = fig_width_pt * golden_ratio
98
+ elif ratio == "square":
99
+ fig_height_pt = fig_width_pt
100
+ elif isinstance(ratio, (int, float)):
101
+ fig_height_pt = fig_width_pt * ratio
102
+ else:
103
+ raise ValueError("Invalid ratio specified.")
104
+ fig_dim = (fig_width_pt, fig_height_pt)
105
+ return fig_dim
106
+
107
+
108
+ def create_lineplot(
109
+ nx_subplots=1,
110
+ ny_subplots=1,
111
+ width=426.79135,
112
+ figsize=None,
113
+ dpi=300,
114
+ ratio="golden",
115
+ gridspec_kw=None,
116
+ ):
117
+ """
118
+ Creates a line plot figure and axes.
119
+ """
120
+ if figsize is not None:
121
+ fig_width, fig_height = figsize
122
+ else:
123
+ fig_width, fig_height = set_size(width, ratio=ratio)
124
+
125
+ if gridspec_kw is None:
126
+ gridspec_kw = {"wspace": 0.08, "hspace": 0.1}
127
+
128
+ fig, axs = plt.subplots(
129
+ ny_subplots,
130
+ nx_subplots,
131
+ figsize=(fig_width, fig_height),
132
+ dpi=dpi,
133
+ constrained_layout=False,
134
+ gridspec_kw=gridspec_kw,
135
+ )
136
+ return fig, axs