10

I'm trying to add a Colorbar to a spectrogram. I have tried every example and question thread I have found online and none have solved this issue

Note that 'spl1' (data splice 1) is a trace from ObsPy.

My code is:

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)

It comes out with the error:

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

Best result so far:

Replacing the last 3 lines above with:

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)

Produces this: Waveform and spectrogram plot

and this error for the colorbar:

axes object has no attribute 'autoscale_None'

I don't seem to be able to find a way to get the colorbar on the right to work.

Solutions?

One of the solutions I have seen is that you need to create an 'image' of your data using imshow(), however I don't get an output for that from Spectrogram(), only 'ax'. I have seen places try with the 'ax,spec' output from spectrogram() but that is causing the TypeError.

I hope someone can give a hand with this - I've been working on this all day now!

Community
  • 1
  • 1
mjp
  • 1,618
  • 2
  • 22
  • 37
  • 3
    Have you successfully plotted a spectrogram without a colorbar? What is the ``spectrogram`` function (from which library) you are using? – gsmafra Feb 16 '16 at 01:18
  • 1
    @gsmafra I have updated the post above with more info - I can get the spectrogram to plot normally yes. The spectrogram function is from: obspy.imaging.spectrogram.spectrogram (as it has simpler built-in functionality) - although underneath it uses specgram – mjp Feb 16 '16 at 15:10
  • 1
    ongoing discussion in: https://github.com/obspy/obspy/issues/1086 which has a successful colorbar plot. Doesn't work for my situation but if a solution is found there then I'll add the solution here too. – mjp Mar 22 '16 at 22:49

1 Answers1

4

Solved it, with help from this link. It doesn't show decibels yet but the primary issue was getting the colorbar:

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

mjp
  • 1,618
  • 2
  • 22
  • 37