mjp*_*mjp 7 python spectrogram python-2.7
我正在尝试将色条添加到光谱图中.我已经尝试了我在网上找到的每一个例子和问题线程,但没有人解决过这个问题
请注意,'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)
Run Code Online (Sandbox Code Playgroud)
它出现了错误:
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
Run Code Online (Sandbox Code Playgroud)
迄今为止的最佳结果:
用以下代码替换上面的最后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)
Run Code Online (Sandbox Code Playgroud)
和colorbar的这个错误:
axes object has no attribute 'autoscale_None'
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到一种方法来让右边的颜色条工作.
解决方案?
我见过的解决方案之一是你需要使用imshow()创建数据的"图像",但是我没有从Spectrogram()获得输出,只有'ax'.我已经看到地方尝试使用spectrogram()的'ax,spec'输出,但这会导致TypeError.
我希望有人可以帮忙 - 我一整天都在努力!
在此链接的帮助下解决了它。它尚未显示分贝,但主要问题是获取颜色条:
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)
Run Code Online (Sandbox Code Playgroud)