0

I want to make a subplot using the input data

manas
  • 479
  • 2
  • 14

1 Answers1

1

I think this is just a question of passing the spectrogram's "mappable" to plt.colorbar() so that it knows what to make a colourbar for. The tricky thing is that it's a bit buried in an attribute of the spectrogram Axes:

fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
ax1.plot(time, data1[0].data)
ax2.plot(time, data2.data)
spec = data2.spectrogram(axes=ax3,  # <-- Assign a name.
                         show=True,
                         samp_rate=20,
                         per_lap=0.5,
                         wlen=30,
                         log=True,
                         cmap='plasma',  # <-- Don't use jet :)
                         clip=(0.05, 0.2),
                        )
plt.xlabel('Time')
plt.ylabel('Frequency')

# More flexibility with the positioning:
cbar_ax = fig.add_axes([0.2, 0.0, 0.6, 0.05])  # Left, bottom, width, height.
cbar = fig.colorbar(spec.collections[0],  # <-- Get the mappable.
                    cax=cbar_ax,
                    orientation='horizontal')
cbar.set_label('Colorbar label')

plt.show()

This also shows how to position the colorbar where you want. And I changed your colourmap to plasma because you shouldn't use jet.

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • Sorry about that, it seems the mappable that `plt.colorbar()` wants is in an attribute. I tried `spec.images[0]` but that thing is empty for some reason, turns out it was lurking in `spec.collections[0]`. I feel like `obspy` could make that more convenient. – Matt Hall Aug 05 '21 at 17:53
  • I edited the answer to show how to position it differently. If you need more help with it, you should probably ask another question. – Matt Hall Aug 05 '21 at 18:14