How do generate spectrogram

How do I use gwpy to plot a spectrogram similar to the gravitational wave events on the GWOSC website, and what parameters should be set.
Such code is here
strain =,format=‘hdf5.gwosc’)

center = int(t0)

strain = strain.crop(center-2, center+1)

dt = 0.3 #-- Set width of q-transform plot, in seconds

hq = strain.q_transform(outseg=(t0 - dt, t0 + dt))

fig4 = hq.plot()

ax = fig4.gca()



try: fig4.colorbar(vmin = 0, vmax = 25)

except: pass
thank you for your solution,i wish you have a nice day!


We show an example of this in the “Quickview Notebook”

Good luck!

Thank you,i have seen this example ,but in other GW events ,i want to know how to plot its spectogram as the same as the picture in GWOSC.Thank you for your reply!

Hi Faker

It looks like you’re using Q-Transform instead of spectrogram?
If you’re using gwpy spectrogram, there’re some parameters to adjust, for example fft length, overlap between windows,etc. Reminder: gwpy’s spectrogram function will normalize the spectorgram by default (median/mean). Normalizations here are done in each frequency bin, by using mean/median value of this bin instead of using fourier transform’s absolute coefficients. Normaliation help us to avoid spectral lines in spectrograms without line removal.

If you want to make plot yourself instead of gwpy plot function, you can first use spectrogram function to generate spectrogram class variable, and then extract the 2-d spectrogram numpy matrix with ‘variable_name.value’ then do whatever you want.
You can try to adjust those parameter in order to obtain the plots meet your requirement. BTW, I believe most of GWOSC plots are Q-Transform based time-frequency plots, you might need to take a look at Q-Transform(gwpy function)/Constant Q transform(librosa is recommended) to have a better understanding of parameters and then adjust them to obtain plots you want.

In gwpy q-transform there’s normalization as well. I suggest you to understand Constant Q-Transform first. Crucial parameters of librosa cqt: Q value (controls the time-frequency resolution), bins per octave (controls frequency bin sampling, in log scale ofc), number of bins and minimum frequency(controls frequency range). Q-transform in gwpy basically sampled multiple Q in Q range, and choose one of them, with some stratagies (for example, the one contains the brightest pixel) to perform CQT.

For data, in order to obtain better image, one should first select a long stationary noise around the target data for high resolution estimated PSD. And use the PSD to whiten the data, gwpy q transform function also contains whitening, but I believe that function uses target data for PSD estimation, which might not always be a good idea.

Best, happy xmas holidays