Open In App

Scipy – Display electrocardiogram

Last Updated : 17 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Electrocardiography is the practice of using electrodes mounted on the skin to produce an electrocardiogram, which is a graph of voltage versus time of the electrical activity of the heart.

Applications:

  • Heartbeat analysis
  • Cancer treatment

In python using scipy we can generate electrocardiogram by using scipy.misc.electrocardiogram()

It is used to load an electrocardiogram and will return only 1-D signal.

The signal which is returned is a 5-minute-long electrocardiogram (ECG), which is a medical recording of the heart’s electrical activity, it basically returns an n -Dimensional array.

Approach:

  • Import numpy and scipy modules.
  • Create an ecg model.
  • Calculate time data with frequency.
  • Display the graph.

Implementations:

Python




# import electrocardiogram
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
  
# import numpy
import numpy as np
  
# define electrocardiogram as ecg model
ecg = electrocardiogram()
  
# frequency is 0
frequency = 360
  
# calculating time data with ecg size along with frequency
time_data = np.arange(ecg.size) / frequency
  
# plotting time and ecg model
plt.plot(time_data, ecg)
plt.xlabel("time in seconds")
plt.ylabel("ECG in milli Volts")
  
# display
plt.show()


Output:

  • Change the x, y limits for clarity visualization.

Python3




# import electrocardiogram
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
  
# import numpy
import numpy as np
  
# define electrocardiogram as ecg model
ecg = electrocardiogram()
  
# frequency is 360
frequency = 360
  
# calculating time data with ecg size along with frequency
time_data = np.arange(ecg.size) / frequency
  
# plotting time and ecg model
plt.plot(time_data, ecg)
plt.xlabel("time in seconds")
plt.ylabel("ECG in milli Volts")
plt.xlim(9, 10.2)
plt.ylim(-1, 1.5)
# display
plt.show()


Output:

  • Display array for ECG:

Python




print(ecg)


Output:

[-0.245 -0.215 -0.185 ... -0.405 -0.395 -0.385]
  • Change the frequency to 1

Python3




#import electrocardiogram
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
  
#import numpy
import numpy as np
  
# define electrocardiogram as ecg model
ecg = electrocardiogram()
  
# frequency is 1
frequency = 1
  
# calculating time data with ecg size along with frequency
time_data = np.arange(ecg.size) / frequency
  
# plotting time and ecg model
plt.plot(time_data, ecg)
plt.xlabel("time in seconds")
plt.ylabel("ECG in milli Volts")
plt.xlim(9, 10.2)
plt.ylim(-1, 1.5)
  
# display
plt.show()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads