-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
388 lines (341 loc) · 11.2 KB
/
Copy pathplotting.py
File metadata and controls
388 lines (341 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from dataclasses import dataclass
import matplotlib
import matplotlib.pyplot as plt
import mpl_fontkit as fk
from matplotlib.axes import Axes
from matplotlib.artist import Artist
from matplotlib.transforms import blended_transform_factory
from mpl_toolkits.mplot3d.axes3d import Axes3D
import os
OUTPUT_DIR = "output/"
fk.install("Noto Sans", verbose=False)
fk.set_font("Noto Sans")
matplotlib.use("qtagg")
plt.rcParams.update(
{
"lines.linewidth": 2,
"text.usetex": True,
"font.size": 20,
"font.family": "sans-serif",
"font.sans-serif": ["Noto Sans"],
# change default color cycle
"axes.prop_cycle": matplotlib.cycler(color=["r", "k", "c", "orange"]),
"text.latex.preamble": "".join(
[
r"\usepackage{amsmath}",
r"\usepackage[cm]{sfmath}", # makes arbitrary fonts compat with latex
r"\usepackage{bm}", # for \boldsymbol{}
]
),
}
)
def get_rows(total: int, cols: int) -> int:
"""
Get number of rows in plot based on total number of subplots and columns
:param total: number of subplots
:param cols: number of columns
:returns: number of rows
"""
rows = total // cols
if total % cols != 0:
rows += 1
return rows
@dataclass
class Vline:
"""Parameters for plotting a vertical line"""
x: float
"""x coordinate of vline"""
y: float
"""y coordinate used for label"""
label: str
"""label to be optionally added to plot"""
color: str = "gray"
"""line color of the vline"""
linestyle: str = "dashed"
"""line style"""
ha: str = "right"
"""horizontal alignment of label"""
va: str = "top"
"""vertical alignment of label"""
class Plot2D:
def __init__(
self,
name: str,
N_subplots: int = 1,
cols: int = 1,
fontsize: int = 20,
sharex: bool = True,
title: bool = True,
gridspec_kw: dict = {},
grid: str | None = None,
):
"""
2D Plot class
:param name: name of the plot for title and filename
:param N_subplots: number of subplots
:param cols: number of columns
:param fontsize: font size
:param sharex: whether to share the x-axis
:param title: whether or not to show the title
:param gridspec_kw: use to specify ratio of subplot sizes
:param grid: specify grid axis for ax.grid(), or None. {'both', 'x', 'y'}
:returns: None
"""
self.name = name
self.N_subplots = N_subplots
self.cols = cols
self.sharex = sharex
self.title = title
plt.rc("font", size=fontsize)
# --- #
self._k = -1
self.colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
self.rows = get_rows(self.N_subplots, self.cols)
self.axs: list[Axes]
self.fig, self.axs = plt.subplots(
self.rows, self.cols, sharex=sharex, gridspec_kw=gridspec_kw
)
height = max(10, 2 * N_subplots)
width = max(10, 5 * cols)
self.fig.set_size_inches(width, height)
if self.N_subplots > 1:
for ax in self.axs:
if grid is not None:
ax.grid(axis=grid)
else:
if grid is not None:
self.axs.grid(axis=grid)
def get_next_subplot(self, xlab: str, ylab: str, ylab_rotation: int = 90) -> Axes:
"""
Get the next subplot and add axis labels
:param xlab: x-axis label
:param ylab: y-axis label
:param ylab_rotation: y-axis label orientation
:returns: Axes object
"""
self._k += 1
# ax = self.fig.add_subplot(self.rows, self.cols, self.k)
if self.N_subplots > 1:
ax = self.axs[self._k]
else:
ax = self.axs
ax.set_xlabel(xlab)
ax.set_ylabel(ylab, rotation=ylab_rotation)
return ax
def plot_vlines(self, ax: Axes, vlines: list[Vline], show_label=True) -> None:
"""
Plot list of Vline objects.
The reason you'd want to do it as a list is that you may want multiple plots with the same vlines representing timesteps
:param ax: Axes object
:param vlines: list of Vlines to be plotted
:param show_label: whether or not to display the vline.label
:returns: None
"""
for vline in vlines:
if show_label:
ax.text(
x=vline.x,
y=vline.y,
s=vline.label,
transform=blended_transform_factory(ax.transData, ax.transAxes),
rotation=90,
ha=vline.ha,
va=vline.va,
)
ax.axvline(
vline.x, color=vline.color, label=vline.label, linestyle=vline.linestyle
)
def add_legend(
self,
ax: Axes,
handles: list[Artist] | None = None,
zorder: int | None = None,
**kwargs,
) -> None:
"""
Add legend without duplicate labels
:param ax: Axes object
:param handles: list of Artists to be added to the legend
:param zorder: drawing order of artists
:returns: None
"""
if handles is None:
handles, labels = plt.gca().get_legend_handles_labels()
else:
labels = [x.get_label() for x in handles]
by_label = dict(zip(labels, handles))
leg = ax.legend(by_label.values(), by_label.keys(), **kwargs)
if zorder is not None:
leg.set_zorder(zorder)
def adjust_hspace(self, hspace: float) -> None:
"""
Specify height of padding between subplots
:param hspace: height of padding
:returns: None
"""
self.fig.subplots_adjust(hspace=hspace)
def set_tight_fig(self) -> None:
"""
Rearrange figure to prevent overlapping labels, etc.
"""
self.fig.tight_layout()
def set_tight_axes(self) -> None:
"""
Remove whitespace within axes objects
"""
plt.axis("tight")
def set_scale_equal(self, ax: Axes) -> None:
"""
Make axes of 2D plot have equal scale but inequal aspect
:param ax: matplotlib axis
:returns: None
"""
x_limits = ax.get_xlim()
y_limits = ax.get_ylim()
x_range = abs(x_limits[1] - x_limits[0])
y_range = abs(y_limits[1] - y_limits[0])
ax.set_box_aspect(y_range / x_range)
def save_img(
self,
show: bool = False,
extension: str = ".png",
keep_open: bool = False,
) -> None:
"""
Save the image to file.
:param show: whether or not to bring up interactive GUI
:param extension: filetype
:param keep_open: prevent plot from being closed
:returns: None
"""
self.set_tight_axes()
self.set_tight_fig()
if self.N_subplots > 1:
for ax in self.axs:
if self.sharex:
ax.label_outer()
os.makedirs(OUTPUT_DIR, exist_ok=True)
plt.savefig(
OUTPUT_DIR + self.name + extension,
dpi=200,
)
if show:
plt.show()
elif not keep_open:
plt.close()
return None
class Plot3D:
def __init__(
self,
name: str,
N_subplots: int = 1,
title: bool = True,
grid: str | None = None,
**kwargs,
):
"""
3D Plot class
:param name: name of the plot for title and filename
:param N_subplots: number of subplots
:param title: whether or not to show the title
:param grid: specify grid axis for ax.grid(), or None. {'both', 'x', 'y', 'z'}
:returns: None
"""
self.name = name
self.N_subplots = N_subplots
self.title = title
# --- #
self._k = -1
self.colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
self.cols = 1
self.rows = get_rows(self.N_subplots, self.cols)
self.fig = plt.figure()
self.fig.set_size_inches(10, 10)
self.axs: list[Axes3D]
if N_subplots == 1:
self.axs = self.fig.add_subplot(projection="3d", **kwargs)
if grid is not None:
self.axs.grid(axis=grid)
else:
self.axs = self.fig.add_subplot(
self.rows, self.cols, projection="3d", **kwargs
)
for ax in self.axs:
if grid is not None:
ax.grid(axis=grid)
def get_next_subplot(
self,
xlab: str,
ylab: str,
zlab: str,
fsize_label: int = 20,
**kwargs,
) -> Axes3D:
"""
Get the next subplot and add axis labels
:param xlab: x-axis label
:param ylab: y-axis label
:param zlab: z-axis label
:param fsize_label: fontsize for labels
:returns: Axes3D object
"""
self._k += 1
# ax = self.fig.add_subplot(self.rows, self.cols, self.k)
if self.N_subplots > 1:
ax = self.axs[self._k]
else:
ax = self.axs
ax.set_xlabel("\n" + xlab, fontsize=fsize_label, **kwargs)
ax.set_ylabel("\n" + ylab, fontsize=fsize_label, **kwargs)
ax.set_zlabel("\n" + zlab, fontsize=fsize_label, **kwargs)
return ax
def set_scale_equal(self, ax: Axes3D) -> None:
"""
Make axes of 3D plot have equal scale but inequal aspect
:param ax: Axes3D object
"""
x_limits = ax.get_xlim3d()
y_limits = ax.get_ylim3d()
z_limits = ax.get_zlim3d()
x_range = abs(x_limits[1] - x_limits[0])
y_range = abs(y_limits[1] - y_limits[0])
z_range = abs(z_limits[1] - z_limits[0])
ax.set_box_aspect((x_range, y_range, z_range))
def add_legend(
self, ax: Axes, handles: list | None = None, zorder: int | None = None, **kwargs
) -> None:
"""
Add legend without duplicate labels
:param ax: Axes3D object
:param handles: list of Artists to be added to the legend
:param zorder: drawing order of artists
:returns: None
"""
if handles is None:
handles, labels = plt.gca().get_legend_handles_labels()
else:
labels = [x.get_label() for x in handles]
by_label = dict(zip(labels, handles))
leg = ax.legend(by_label.values(), by_label.keys(), **kwargs)
if zorder is not None:
leg.set_zorder(zorder)
def save_img(
self,
show: bool = False,
extension: str = ".png",
keep_open: bool = False,
) -> None:
"""
Save the image to file.
:param show: whether or not to bring up interactive GUI
:param extension: filetype
:param keep_open: prevent plot from being closed
:returns: None
"""
os.makedirs(OUTPUT_DIR, exist_ok=True)
plt.savefig(OUTPUT_DIR + self.name + extension, dpi=200)
if show:
plt.show()
elif not keep_open:
plt.close()
return None