-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
50 lines (37 loc) · 1.74 KB
/
plot.py
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
import matplotlib.pyplot as plt
import numpy as np
lead_index = ["I", "II", "III", "aVR", "aVL", "aVF", "V1", "V2", "V3", "V4", "V5", "V6"]
def plot_12(ecg, sampling_rate, plot_grid=True):
"This function is ridiculously slow and there are mutch better ways to implemt"
figsize = (25, 1.5 * 10)
height_ratios = [ecg[:, i].max() - ecg[:, i].min() for i in range(12)]
height_ratios = [i / height_ratios[0] for i in height_ratios]
fig, axs = plt.subplots(12, 1, figsize=figsize, gridspec_kw={"height_ratios": height_ratios})
fig.subplots_adjust(hspace=0.0)
for i, ax in enumerate(axs):
plot_ax(ax, ecg[:, i], sampling_rate, plot_grid=plot_grid)
ax.set_ylabel(lead_index[i], fontsize=13)
return fig, axs
def plot_ax(ax, signal, sampling_rate, plot_grid=True):
color_line = (0, 0, 0.7)
color_major = (1, 0, 0)
color_minor = (1, 0.7, 0.7)
ax.plot(np.arange(0, len(signal)), signal, linewidth=0.5, color=color_line)
if plot_grid:
# set major grid
xmajor = np.arange(0, len(signal), sampling_rate * 0.2)
ymajor = np.arange(signal.min(), signal.max(), 0.5)
ax.set_xticks(xmajor, minor=False)
ax.set_xticks(xmajor, minor=False)
ax.set_yticks(ymajor, minor=False)
ax.grid(which="major", color=color_major, linewidth=0.5)
# set minor grid
xminor = np.arange(0, len(signal), sampling_rate * 0.04)
yminor = np.arange(signal.min(), signal.max(), 0.1)
ax.set_xticks(xminor, minor=True)
ax.set_yticks(yminor, minor=True)
ax.grid(which="minor", color=color_minor, linewidth=0.5)
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.margins(0)
ax.set_ylim(signal.min(), signal.max())