title: Adding Colorbar to a Spectrogram
body: <p>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</p>
  
  <p>Note that 'spl1' (data splice 1) is a trace from ObsPy.</p>
  
  <p><strong>My code is:</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>It comes out with the error:</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-18-61226ccd2d85&gt;", line 14, in &lt;module&gt;
      ax,spec = spectrogram(spl1[0].data,spl1[0].stats.sampling_rate, show=False, axes=ax2)
  
  TypeError: 'Axes' object is not iterable
  </code></pre>
  
  <p><strong>Best result so far:</strong></p>
  
  <p>Replacing the last 3 lines above with:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Produces this:
  <a href="https://i.stack.imgur.com/0ZZYR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ZZYR.png" alt="Waveform and spectrogram plot"></a></p>
  
  <p>and this error for the colorbar:</p>
  
  <pre><code>axes object has no attribute 'autoscale_None'
  </code></pre>
  
  <p>I don't seem to be able to find a way to get the colorbar on the right to work.</p>
  
  <p><strong>Solutions?</strong></p>
  
  <p>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.</p>
  
  <ul>
  <li>Very similar code I found but didn't work <a href="https://www.nicotrebbin.de/wp-content/uploads/2012/03/bachelorthesis.pdf" rel="nofollow noreferrer">https://www.nicotrebbin.de/wp-content/uploads/2012/03/bachelorthesis.pdf</a> (ctrl+f 'colorbar')</li>
  <li>Looked at code examples <a href="https://stackoverflow.com/questions/20053840/error-while-attempting-to-add-color-bar">from a related question</a></li>
  <li>imshow() <a href="https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar">suggestions</a> and <a href="http://docs.obspy.org/tutorial/code_snippets/continuous_wavelet_transform.html" rel="nofollow noreferrer">example</a> - can't get an output from spectrogram to turn into an image. That second link, I also can't get the mlpy module to work (it doesn't think there's a mlpy.wavelet function)</li>
  <li>This problem was addressed in <a href="https://github.com/obspy/obspy/issues/1086" rel="nofollow noreferrer">an improvement post for obspy</a> but the solution he stated he found was not given</li>
  </ul>
  
  <p>I hope someone can give a hand with this - I've been working on this all day now!</p>