PPSD.plot_spectrogram

Hello
I checked this thread:

And it didn’t work for me…
AttributeError: ‘NoneType’ object has no attribute ‘axes’

Is there another way to customise axis limits, fig size, resolution etc?

Thanks
Dave

Not sure what your actual code looks like, so can only make an educated guess… but if whatever plot doesn’t return the figure instance (thats why you work on a None is my guess), you can still get the figure using matplotlib API, e.g. pyplot.gcf() or pyplot.gca()

Also, if you are in IPython or something similar, the interactive showing of Figures immediately can mess with all that too

Thanks for this; that helps a lot. Labels, ticks, limits etc.
But I’d also like to make it linear on the y-axis…
Any clues? If I go through matplotlib (e.g., plt.yscale(‘linear’)) it just ignores it.
Cheers,
Dave

Dunno… this works for me:

import matplotlib.pyplot as plt
from obspy.imaging.tests.test_ppsd import _get_ppsd
ppsd = _get_ppsd()
ppsd.plot_spectrogram?
fig = ppsd.plot_spectrogram(show=False)
fig.axes
ax = fig.axes[0]
ax.set_yscale('linear')
plt.show()

You could toss in a plt.draw() before the show() to force an update, but it might not even be needed, looks like.

Your plt.yscale(‘linear’) acts on the last active Axes so you might’ve called it on the colorbar or something (which is an Axes too, check out fig.axes contents in an IPython if curious)

That does work - thanks.
But I’m still stuck with a non-linear y-scale in terms of frequency…
To get a linear y-scale for frequency, would I have to use the st.spectrogram() function instead?

Confused… it works to set y-scale linear, but you’re still stuck with a non linear y-scale?
st.spectrogram()? I thought we are talking about PPSD.plot_spectrogram()?

Sorry, my fault.
The plot I really want is a spectrogram from the PPSD, with a linear frequency y-axis.

But because Period = 1 / Frequency is not linear, I guess that is not so easy from the data structure of the PPSD object.

Ah, gotcha. Sadly, that’s not implemented currently, so for now you’d probably have to do it yourself. Way to go would probably be subclassing PPSD and overwriting the plot_spectrogram method…

from obspy.signal import PPSD

class MyPPSD(PPSD):
    def plot_spectrogram(self, ...):
        # copy in the code from obspy for this method and start tweaking it

Then use that MyPPSD as a drop in replacement for PPSD in your codes.

It shouldn’t be too hard to figure out what to change in the code, you can look at imaging/spectrogram.py for different means to plot those data and also look for traces of xaxis_frequency switch in signal/spectral_estimation.py for how to tweak things.

Ok, that makes sense. Vielen dank.

1 Like

Ìf you end up implementing this for yourself, feel free to open a PR for this :+1: