Skip to content

Added a save_as_pdf function #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Plot standard ECG chart from data.
* Support both direct plotting and plotting SVG preview in browser (currently only works on mac)
* Support saving PNG and SVG to disk
* Support saving PNG, JPG, SVG or PDF to disk
* Support customer defined lead order
* Support customer defined column count

Expand Down Expand Up @@ -44,7 +44,7 @@ params:
import ecg_plot

ecg = load_data() # load data should be implemented by yourself
ecg_plot.plot(ecg, sample_rate = 500, title = 'ECG 12')
ecg_plot.plot(ecg, sample_rate=500, title='ECG 12')
ecg_plot.show()

```
Expand All @@ -55,7 +55,7 @@ ecg_plot.show()
import ecg_plot

ecg = load_data() # load data should be implemented by yourself
ecg_plot.plot_1(ecg[1], sample_rate=500, title = 'ECG')
ecg_plot.plot_1(ecg[1], sample_rate=500, title='ECG')
ecg_plot.show()
```

Expand All @@ -65,8 +65,19 @@ ecg_plot.show()
import ecg_plot

ecg = load_data() # load data should be implemented by yourself
ecg_plot.plot_12(ecg, sample_rate = 500, title = 'ECG 12')
ecg_plot.save_as_png('example_ecg','tmp/')
ecg_plot.plot_12(ecg, sample_rate=500, title='ECG 12')
ecg_plot.save_as_png('example_ecg', 'tmp/')

```

#### Save result as pdf

```
import ecg_plot

ecg = load_data() # load data should be implemented by yourself
fig = ecg_plot.plot_12(ecg, sample_rate = 500, title = 'ECG 12')
ecg_plot.save_as_pdf('example_ecg', fig, 'tmp/')

```

Expand Down
2 changes: 1 addition & 1 deletion ecg_plot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .ecg_plot import plot_12, plot_1, show, show_svg, save_as_png, save_as_svg, save_as_jpg, plot
from .ecg_plot import plot_12, plot_1, show, show_svg, save_as_png, save_as_svg, save_as_jpg, save_as_pdf, plot
47 changes: 44 additions & 3 deletions ecg_plot/ecg_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def plot_12(
speed : signal speed on display, defaults to 50 mm / sec
voltage : signal voltage on display, defaults to 20 mm / mV
line_width : line width, default to 0.6

# Returns
fig : figure object
"""
if not lead_order:
lead_order = list(range(0,len(ecg)))
Expand Down Expand Up @@ -88,6 +91,7 @@ def plot_12(
t_ax.tick_params(axis='x',rotation=90)

_ax_plot(t_ax, np.arange(0, len(ecg[t_lead])*step, step), ecg[t_lead], seconds)
return fig

def plot(
ecg,
Expand Down Expand Up @@ -116,6 +120,9 @@ def plot(
show_lead_name : show lead name
show_grid : show grid
show_separate_line : show separate line

# Returns
fig : figure object
"""

if not lead_order:
Expand Down Expand Up @@ -154,8 +161,25 @@ def plot(
color_line = (0,0,0.7)

if(show_grid):
ax.set_xticks(np.arange(x_min,x_max,0.2))
ax.set_yticks(np.arange(y_min,y_max,0.5))
ax.set_xticks(np.arange(x_min, x_max, 0.2))
ax.set_yticks(np.arange(y_min, y_max, 0.5))

# Set tick labels only at every 1.0 interval
xtick_labels = []
for val in np.arange(x_min, x_max, 0.2):
if np.isclose(val % 1.0, 0, atol=1e-8):
xtick_labels.append(f"{val:g}")
else:
xtick_labels.append("")
ax.set_xticklabels(xtick_labels)

ytick_labels = []
for val in np.arange(y_min, y_max, 0.5):
if np.isclose((val - y_min) % 1.0, 0, atol=1e-8):
ytick_labels.append(f"{val:g}")
else:
ytick_labels.append("")
ax.set_yticklabels(ytick_labels)

ax.minorticks_on()

Expand Down Expand Up @@ -193,6 +217,7 @@ def plot(
linewidth=line_width * display_factor,
color=color_line
)
return fig


def plot_1(ecg, sample_rate=500, title = 'ECG', fig_width = 15, fig_height = 2, line_w = 0.5, ecg_amp = 1.8, timetick = 0.2):
Expand All @@ -203,8 +228,11 @@ def plot_1(ecg, sample_rate=500, title = 'ECG', fig_width = 15, fig_height = 2,
title : Title which will be shown on top off chart
fig_width : The width of the plot
fig_height : The height of the plot

# Returns
fig : figure object
"""
plt.figure(figsize=(fig_width,fig_height))
fig = plt.figure(figsize=(fig_width,fig_height))
plt.suptitle(title)
plt.subplots_adjust(
hspace = 0,
Expand All @@ -220,6 +248,7 @@ def plot_1(ecg, sample_rate=500, title = 'ECG', fig_width = 15, fig_height = 2,
#plt.rcParams['lines.linewidth'] = 5
step = 1.0/sample_rate
_ax_plot(ax,np.arange(0,len(ecg)*step,step),ecg, seconds, line_w, ecg_amp,timetick)
return fig

DEFAULT_PATH = './'
show_counter = 1
Expand Down Expand Up @@ -270,3 +299,15 @@ def save_as_jpg(file_name, path = DEFAULT_PATH):
plt.ioff()
plt.savefig(path + file_name + '.jpg')
plt.close()

def save_as_pdf(file_name, fig, path = DEFAULT_PATH):
"""Plot multi lead ECG chart.
# Arguments
file_name: file_name
path : path to save image, defaults to current folder
"""
plt.ioff()
fig.savefig(os.path.join(path, file_name + '.pdf'),
format='pdf',
bbox_inches='tight')
plt.close(fig)
3 changes: 2 additions & 1 deletion ecg_plot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def load_ecg_from_mat(file_path):

# ecg_plot.plot(test_ecg, sample_rate = 500, title = '', columns = 1,show_grid = False, show_lead_name = False, style='bw')
# ecg_plot.plot(test_ecg, sample_rate = 500, title = '', columns = 3)
ecg_plot.plot(test_ecg, title='test')
ecg = ecg_plot.plot(test_ecg, title='test')
ecg_plot.save_as_png('example_ecg_1','tmp/')
ecg_plot.save_as_pdf('example_ecg_1', ecg,'tmp/')
ecg_plot.show()