我正在尝试使用scipy的inetrp2d函数对从matplotlib获得的光谱图进行插值,但不知何故未能得到相同的光谱图。数据可用here
实际的光谱图是:
python - 与matplotlib进行2D插值频谱图差异的原因是什么?-LMLPHP
插值谱图为:
python - 与matplotlib进行2D插值频谱图差异的原因是什么?-LMLPHP
代码看起来没问题,但即使这样也有问题。使用的代码是:

from __future__ import division
from matplotlib import ticker as mtick
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
from bisect import bisect
from scipy import interpolate
from matplotlib.ticker import MaxNLocator
data = np.genfromtxt('spectrogram.dat', skiprows = 2, delimiter = ',')
pressure = data[:, 1] * 0.065
time = data[:, 0]
cax = plt.specgram(pressure * 100000, NFFT = 256, Fs = 50000, noverlap=4, cmap=plt.cm.gist_heat, zorder = 1)

f = interpolate.interp2d(cax[2], cax[1], cax[0], kind='cubic')
xnew = np.linspace(cax[2][0], cax[2][-1], 100)
ynew = np.linspace(cax[1][0], cax[1][-1], 100)
znew = 10 * np.log10(f(xnew, ynew))

fig = plt.figure(figsize=(6, 3.2))
ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.pcolormesh(xnew, ynew, znew, cmap=plt.cm.gist_heat)
# plt.colorbar()
plt.title('Interpolated spectrogram')
plt.colorbar(orientation='vertical')
plt.savefig('interp_spectrogram.pdf')

如何用Python正确地插入谱图?

最佳答案

解决方案的关键在于此警告,您可能看到,也可能没有看到:

RuntimeWarning: invalid value encountered in log10
    znew = 10 * np.log10(f(xnew, ynew))

如果您的数据实际上是一个幂,您希望将其日志显式地查看为分贝幂,请在拟合到样条曲线之前,首先获取该日志:
spectrum, freqs, t, im = cax
dB = 10*np.log10(spectrum)
#f = interpolate.interp2d(t, freqs, dB, kind='cubic') # docs for this recommend next line
f = interpolate.RectBivariateSpline(t, freqs,  dB.T) # but this uses xy not ij, hence the .T

xnew = np.linspace(t[0], t[-1], 10*len(t))
ynew = np.linspace(freqs[0], freqs[-1], 10*len(freqs)) # was it wider spaced than freqs on purpose?
znew = f(xnew, ynew).T

然后按你的方式绘制:
python - 与matplotlib进行2D插值频谱图差异的原因是什么?-LMLPHP
上一个答案:
如果您只想plot on logscale,请使用matplotlib.colors.LogNorm
znew = f(xnew, ynew) # Don't take the log here

plt.figure(figsize=(6, 3.2))
plt.pcolormesh(xnew, ynew, znew, cmap=plt.cm.gist_heat, norm=colors.LogNorm())

看起来是这样的:
python - 与matplotlib进行2D插值频谱图差异的原因是什么?-LMLPHP
当然,当在对数刻度上绘制时,它的值为负时仍然有间隙。当值为负时,数据对您意味着什么应该决定如何填写。一个简单的解决方案是将这些值设置为最小的正值,然后将其填充为黑色:
python - 与matplotlib进行2D插值频谱图差异的原因是什么?-LMLPHP

关于python - 与matplotlib进行2D插值频谱图差异的原因是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32565343/

10-12 21:11