gpt4 book ai didi

python - python,matplotlib:谱图数据数组值与谱图图不匹配

转载 作者:行者123 更新时间:2023-12-03 01:38:49 43 4
gpt4 key购买 nike

我正在使用matplotlib.pyplot.specgram和matplotlib.pyplot.pcolormesh绘制地震信号的频谱图。

背景信息-使用pcolormesh的原因是我需要对频谱图数据数组进行算术运算,然后重新绘制最终的频谱图(对于三分量地震图-东,北和垂直-我需要计算水平频谱幅值和将垂直光谱除以水平光谱)。使用频谱图阵列数据比在单个振幅频谱上更容易做到这一点

我发现算术后的频谱图图具有意外的值。经过进一步调查,结果发现,与使用pyplot.pcolormesh和从pyplot.specgram方法返回的数据数组制作的频谱图相比,使用pyplot.specgram方法制作的频谱图具有不同的值。两个图/数组都应包含相同的值,我无法弄清楚为什么不包含它们。

例:
的情节

plt.subplot(513)
PxN, freqsN, binsN, imN = plt.specgram(trN.data, NFFT = 20000, noverlap = 0, Fs = trN.stats.sampling_rate, detrend = 'mean', mode = 'magnitude')
plt.title('North')
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
plt.clim(0, 150)
plt.colorbar()
#np.savetxt('PxN.txt', PxN)

看起来与
plt.subplot(514)
plt.pcolormesh(binsZ, freqsZ, PxN)
plt.clim(0,150)
plt.colorbar()

即使“PxN”数据数组(即每个段的频谱图数据值)是通过第一种方法生成的,并在第二种方法中重复使用的。

有人知道为什么会这样吗?

附言我意识到我的NFFT值不是平方数,但是在我编码的这个阶段并不重要。

P.P.S.我不知道什么是“imN”数组(从pyplot.specgram返回的第四个变量)以及它用于...。

最佳答案

首先,让我们展示您正在描述的示例,以便其他人

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

# Brownian noise sequence
x = np.random.normal(0, 1, 10000).cumsum()

fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8, 10))

values, ybins, xbins, im = ax1.specgram(x, cmap='gist_earth')
ax1.set(title='Specgram')
fig.colorbar(im, ax=ax1)

mesh = ax2.pcolormesh(xbins, ybins, values, cmap='gist_earth')
ax2.axis('tight')
ax2.set(title='Raw Plot of Returned Values')
fig.colorbar(mesh, ax=ax2)

plt.show()

enter image description here

幅度差异

您会立即注意到绘制值的大小差异。

默认情况下, plt.specgram不会绘制返回的“原始”值。相反,它将它们缩放为分贝(换句话说,它绘制幅度的 10 * log10)。如果您不想扩大规模,则需要指定 scale="linear"。但是,对于查看频率组成,对数刻度将是最有意义的。

考虑到这一点,让我们模仿 specgram的作用:
plotted = 10 * np.log10(values)

fig, ax = plt.subplots()
mesh = ax.pcolormesh(xbins, ybins, plotted, cmap='gist_earth')

ax.axis('tight')
ax.set(title='Plot of $10 * log_{10}(values)$')
fig.colorbar(mesh)

plt.show()

enter image description here

改用对数色标

另外,我们可以在图像上使用对数范数,并获得相似的结果,但可以更清楚地传达出颜色值在对数刻度上:
from matplotlib.colors import LogNorm

fig, ax = plt.subplots()
mesh = ax.pcolormesh(xbins, ybins, values, cmap='gist_earth', norm=LogNorm())

ax.axis('tight')
ax.set(title='Log Normalized Plot of Values')
fig.colorbar(mesh)

plt.show()

enter image description here
imshowpcolormesh
最后,请注意,我们显示的示例没有应用插值,而原始的 specgram图却进行了插值。 specgram使用 imshow,而我们一直在使用 pcolormesh进行绘图。在这种情况下(常规网格间距),我们可以使用其中任何一个。

在这种情况下, imshowpcolormesh都是非常好的选择。但是,如果使用大型数组,则 imshow将具有明显更好的性能。因此,即使您不希望插值(例如 interpolation='nearest'关闭插值),也可以考虑使用它。

举个例子:
extent = [xbins.min(), xbins.max(), ybins.min(), ybins.max()]

fig, ax = plt.subplots()
mesh = ax.imshow(values, extent=extent, origin='lower', aspect='auto',
cmap='gist_earth', norm=LogNorm())

ax.axis('tight')
ax.set(title='Log Normalized Plot of Values')
fig.colorbar(mesh)

plt.show()

enter image description here

关于python - python,matplotlib:谱图数据数组值与谱图图不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49239647/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com