我在保存數字時遇到問題。此代碼對信號進行 FFT,然后應將 FFT 保存在 .png 中的單獨檔案中。第一張圖沒問題,但下一張圖上有所有以前的FFT,也許這是一個典型的問題,你知道如何解決嗎?
import numpy as np
import matplotlib.pyplot as plt
import glob
txt_files = glob.glob("*.pom") #format of files
print(txt_files)
for i in range(len(txt_files)):
mat = np.genfromtxt(txt_files[i])
#x = np.delete(mat, [0, 6] , axis=0)
a = np.delete(mat, 0, axis=1)
b = mat[1, 0] - mat[0, 0]
std = a.std()
mean = a.mean()
print(std, mean)
Fs = 5000 # sumpling freq IF TIME IN FIRST COLUMN
tstep = 1 / Fs # sumple time interval
N = np.size(a) # number of samples
t = np.linspace(0, (N - 1) * tstep, N) # time step
fstep = Fs / N
f = np.linspace(0, (N - 1) * fstep, N) # freq step
X = np.fft.fft2(a)
X_mag = np.abs(X) / N
f_plot = f[0:int(N / 2 1)]
X_mag_plot = 2 * X_mag[0:int(N / 2 1)]
X_mag_plot[0] = X_mag_plot[0] / 2
plt.plot(f_plot, X_mag_plot, "-k", linewidth=0.5)
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude')
plt.xscale("log")
plt.axis([1, 1000, 0, 0.01])
plt.grid(True)
#plt.text(45, .025, r'$\mu=100,\ \sigma=15$', backgroundcolor="w")
plt.text(300, 0.01, f'std={std:.3f} \nmean={mean:.3f}', backgroundcolor="w")
#plt.show()
np.savetxt(txt_files[i] "_Widmo.txt", X_mag_plot, delimiter="\t")
plt.savefig(txt_files[i] ".png", dpi=250)
if i == 1:
np.savetxt(txt_files[0] "_Fs.txt", f_plot, delimiter="\t")
del f_plot
del X_mag_plot
del txt_files[i]
uj5u.com熱心網友回復:
我認為問題在于您繼續以相同的數字作業。嘗試執行以下操作之一:
在回圈的每次迭代中打開一個新圖窗。這不是最佳選擇,因為您可能會打開很多數字:
plt.figure()
另一個更優化的選項是在保存圖形后在每次迭代中關閉圖形:
plt.close('all')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/345408.html
標籤:Python 蟒蛇-3.x matplotlib 快进
