0

There is a vector which i have done stft transform on it in python and i got an image as output. I am trying to save it in a directory but as it has three parameters(f, t, Zxx), i don't know how to save it. thanks if anyone can help me to save it thanks if anyone can help me to save it. my code is as below

#Importing the Libraries-----------------------------------------------------------------------
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat

#Importing the Data----------------------------------------------------------------------------

train_data = loadmat('D:\\thesis\\new data 
preproceses\\A_train_segmented_onehot\\Data_train_3ch_A_train.mat')
train_data = train_data.get('Data_train_3ch_A_train')

#Defiening the parameters----------------------------------------------------------------------
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)

#Calculate STFT---------------------------------------------------------------------------------
f, t, Zxx = signal.stft(train_data[0], fs, nperseg=20)

#Plot Image ------------------------------------------------------------------------------------    
plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=amp, shading='gouraud')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
martineau
  • 119,623
  • 25
  • 170
  • 301
sepehr
  • 9
  • 4

1 Answers1

0

There is already a builtin function in maplotlib / pyplot to save the plot as an image, this will be probably easier for your issue:

Before plt.show(), use savefig function:

plt.savefig('example.png')
Pac0
  • 21,465
  • 8
  • 65
  • 74
  • Thank you so much. may you explain it as this case? what should be instead of 'example.png' – sepehr Oct 18 '20 at 18:13
  • 1
    I don't really know what to explain, except that this function just does what you need: it saves the plot as a .png file (instead of displaying it on the screen). Of course, you can tweak some options (see the documentation link in the answer). See also: https://stackoverflow.com/questions/45691538/how-to-save-a-pcolormesh-image-from-matplotlib . Do you have a more specific point that is unclear? – Pac0 Oct 18 '20 at 18:18