简体   繁体   中英

How to save wfdb plot as an image in local directory?

I want to be able to save wfdb ECG plots as an image file locally to be used for image processing processes. I am using Jupyter notebook on Anaconda, in python programming language.

I have tried plt.savefig() to save the image, and it does generate a file. However when I open the said file, there is no image.

import wfdb
import matplotlib.pyplot as plt

record =wfdb.rdrecord(r'D:\User\qt-database-1.0.0\sel33', sampfrom=0, sampto=999, channels = [1])
#annotation = wfdb.rdann(r'D:\User\qt-database-1.0.0\sel33', 'atr', sampto=1000)

wfdb.plot_wfdb(record=record, 
               #annotation=annotation, 
               plot_sym=True,
               time_units='seconds', 
               title='',
               figsize=(20,3), 
               ecg_grids='')

plt.savefig("foo.png")

The file is also a continuous waveform, and I intend to create a set of images from the waveform. Is there any way to do this?

PS I did manually save the image using snipping tool, however I have found it to be inefficient and time consuming. A code-oriented solution would be a major help.

Within the wfdb.plot() function, there is an optional parameter called return_fig which returns the matplotlib figure that you are trying to save. If you change your code to have wfdb.plot() return the figure, you should be able to save it.

import wfdb
import matplotlib.pyplot as plt

record = wfdb.rdrecord(r'D:\User\qt-database-1.0.0\sel33', sampfrom=0, sampto=999, channels = [1])
#annotation = wfdb.rdann(r'D:\User\qt-database-1.0.0\sel33', 'atr', sampto=1000)

myFigure = wfdb.plot_wfdb(record=record, 
               #annotation=annotation, 
               plot_sym=True,
               time_units='seconds', 
               title='',
               figsize=(20,3), 
               ecg_grids='')

myFigure.savefig("foo.png")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM