我正在尝试将 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)
颜色条的这个错误:
axes object has no attribute 'autoscale_None'
我似乎无法找到让右侧的颜色条工作的方法。
解决方案?
我见过的解决方案之一是您需要使用 imshow() 创建数据的“图像”,但是我没有从 Spectrogram() 获得输出,只有“ax”。我见过一些地方尝试使用 spectrogram() 的“ax,spec”输出,但这会导致 TypeError。
- 我找到了非常相似的代码但没有用https://www.nicotrebbin.de/wp-content/uploads/2012/03/bachelorthesis.pdf (ctrl+f '颜色栏')
- 查看代码示例 from a related question
- imshow() suggestions和 example - 无法从频谱图获得输出以转换为图像。第二个链接,我也无法让 mlpy 模块工作(它认为没有 mlpy.wavelet 函数)
- 此问题已在 an improvement post for obspy 中解决但是没有给出他说他找到的解决方案
我希望有人可以帮助解决这个问题 - 我已经为此工作了一整天了!
最佳答案
在 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)
关于python - 将颜色条添加到频谱图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420052/