-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
101 lines (77 loc) · 2.33 KB
/
plot.py
File metadata and controls
101 lines (77 loc) · 2.33 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
from matplotlib import pyplot as plt
from matplotlib import axes
from matplotlib.animation import FuncAnimation
from functools import partial
"""
This module is a wrapper for matplotlib to make plotting simpler
(matplotlib sucks btw)
"""
class _Ax:
def __init__(self, fig):
self._Fig = fig
def __getitem__(self, coords):
return self._ax(*coords)
def _ax(self, x, y):
self._Fig.axes[x, y] = self._Fig._fig.add_subplot(self._Fig._gs[x, y])
return self._Fig.axes[x, y]
class Figure:
def __init__(self, x, y):
self._fig = plt.figure(figsize=(2*y, 2*x))
self.gridspec = (x, y)
self.gridspec.update(wspace=0.05, hspace=0.05)
self.ax = _Ax(self)
self.axes = {}
self._shown = False
def __enter__(self):
try:
return self
except Exception as e:
print(e)
def __exit__(self, type, value, traceback):
try:
if traceback is None:
if not self._shown: self._fig.show(); self._fig.canvas.draw()
self._shown = True
input("Press any key to close the figure")
plt.close()
else:
raise
except:
import sys
if sys.exc_info()[1] is not value:
raise
def set_imshow(self, ax, array):
ax.get_images()[0].set_data(array)
@property
def gridspec(self):
return self._gs
@gridspec.setter
def gridspec(self, shape):
self._gs = self._fig.add_gridspec(*shape)
def show(self):
if not self._shown: self._fig.show(); self._fig.canvas.draw()
self._shown = True
def pause(self, t):
plt.pause(t)
def update(self, t=0.001):
self.pause(t)
def save(self, path):
self._fig.savefig(path)
# unused
def animate(
self,
update_func,
frames=100,
repeat=True,
repeat_delay=2000,
init=lambda: None,
interval=100, # frame length in ms
blit=False,
):
anim = FuncAnimation(
fig=self._fig, func=update_func, frames=frames,
repeat=repeat, repeat_delay=repeat_delay,
init_func=init, interval=interval, blit=blit,
)
plt.show()
plt.close()