python - 将颜色条添加到频谱图中

标签 python python-2.7 spectrogram

我正在尝试将 Colorbar 添加到频谱图中。我已经尝试了我在网上找到的每个示例和问题线程,但没有一个解决了这个问题

请注意,'spl1'(数据拼接 1)是来自 ObsPy 的跟踪。

我的代码是:

fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.75, 0.7, 0.2]) #[left bottom width height]
ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.60], sharex=ax1)
ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6])

t = np.arange(spl1[0].stats.npts) / spl1[0].stats.sampling_rate
ax1.plot(t, spl1[0].data, 'k')

ax,spec = spectrogram(spl1[0].data,spl1[0].stats.sampling_rate, show=False, axes=ax2)
ax2.set_ylim(0.1, 15)
fig.colorbar(spec, cax=ax3)

出现错误:

Traceback (most recent call last):

  File "<ipython-input-18-61226ccd2d85>", line 14, in <module>
    ax,spec = spectrogram(spl1[0].data,spl1[0].stats.sampling_rate, show=False, axes=ax2)

TypeError: 'Axes' object is not iterable

迄今为止最好的结果:

将上面的最后 3 行替换为:

ax = spectrogram(spl1[0].data,spl1[0].stats.sampling_rate, show=False, axes=ax2)
ax2.set_ylim(0.1, 15)
fig.colorbar(ax,cax=ax3)

产生这个: Waveform and spectrogram plot

颜色条的这个错误:

axes object has no attribute 'autoscale_None'

我似乎无法找到让右侧的颜色条工作的方法。

解决方案?

我见过的解决方案之一是您需要使用 imshow() 创建数据的“图像”,但是我没有从 Spectrogram() 获得输出,只有“ax”。我见过一些地方尝试使用 spectrogram() 的“ax,spec”输出,但这会导致 TypeError。

我希望有人可以帮助解决这个问题 - 我已经为此工作了一整天了!

最佳答案

this link 的帮助下解决了它.它还没有显示分贝,但主要问题是获取颜色条:

from obspy.imaging.spectrogram import spectrogram
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.75, 0.7, 0.2]) #[left bottom width height]
ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.60], sharex=ax1)
ax3 = fig.add_axes([0.83, 0.1, 0.03, 0.6])

#make time vector
t = np.arange(spl1[0].stats.npts) / spl1[0].stats.sampling_rate

#plot waveform (top subfigure)    
ax1.plot(t, spl1[0].data, 'k')

#plot spectrogram (bottom subfigure)
spl2 = spl1[0]
fig = spl2.spectrogram(show=False, axes=ax2)
mappable = ax2.images[0]
plt.colorbar(mappable=mappable, cax=ax3)

produced figure

关于python - 将颜色条添加到频谱图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420052/

相关文章:

python - 确定多记录html形式的记录

python - 这里发生了什么?在随机列表列表中重复行

python-2.7 - 给定所需的分布和经验采样生成随机数

python - 模拟光标从 X,Y 到 X,Y 的运动

python - Scipy.signal.spectrogram输出长度

python - 如何将此代码从 Python 2.7 转换为 Python 3.5 以修复 ---> AttributeError : '_io.TextIOWrapper' object has no attribute 'next'

Python 日期时间精度

python - CNN 中的 8 个预定义二进制卷积滤波器 - Tensorflow

python - 如何在 python 中生成一维信号的频谱图?

python - 理解 scipy.signal.spectrogram() 的输出