Home > Enterprise >  Spectrogram Colorbar as subplot plotted but size not reflecting
Spectrogram Colorbar as subplot plotted but size not reflecting

Time:06-03

I'm trying to get a Spectrogram plot with colorbar like this for a .wav file:

Desired Spectrogram With Colorbar Output

So, I wrote the following:

from matplotlib.figure import Figure
from scipy.io.wavfile import read as readWav

wavfile1 = 'Speech.wav'
wavFs, wavData = readWav(wavfile1)

figSG = Figure(figsize=[15, 8], facecolor='lightgrey')
axSG = figSG.add_subplot(2, 1, 1, position=[0, 0, 1, 0.85])
axCB = figSG.add_subplot(2, 1, 2, position=[0.2, 0.0, 0.6, 0.05])
pltlinescurves = axSG.specgram(wavData, NFFT=256, Fs=8000)
figSG.colorbar(pltlinescurves[3], label='Intensity (in dB)', cax=axCB, orientation='horizontal')

which gives me this:

My Output

As evident from the code above, I have added the position argument as mentioned enter image description here

CodePudding user response:

I managed to use Jody's inputs above and solve the problem partially:

from matplotlib.figure import Figure
from scipy.io.wavfile import read as readWav

wavfile1 = 'Speech.wav'
wavFs, wavData = readWav(wavfile1)

figSG = Figure(figsize=[15, 8], facecolor='lightgrey')
axSG, axCB = figSG.subplots(2, 1, gridspec_kw={'height_ratios': [1, 0.1], 'width_ratios': [1]})
axSG.set_xlabel('Time (s)', fontsize=14)
axSG.set_ylabel('Frequency (Hz)', fontsize=14)
axSG.set_title('Spectrogram (Hz vs s)', fontsize=18)
pltlinescurves = axSG.specgram(wavData, NFFT=256, Fs=wavFs)
cbar = figSG.colorbar(pltlinescurves[3], cax=axCB, orientation='horizontal', pad=1.5)
cbar.set_label('Intensity (in dB)',size=14)
cbaspect = get_aspect(ax=axCB) * 0.5
axCB.set_aspect(cbaspect, anchor='C')
figSG.tight_layout()

where the function get_aspect() used above is from ParitalOutputSolution

Still, I'm unable to shrink the colorbar (to about 75%-85% would be ideal) as shown in the post above. Any leads would be much appreciated.

  • Related