Modify stream plot labels and ticks

Is it possible to modify the ticks and labels of a stream plot?

I want to do the following to this plot:

  • Change the title
  • Remove the tick labels on the Y axes
  • Remove the station and channel labels
  • Remove the lines between the subplots
  • Remove the red comment

All of that you can do with matplotlib API on the Figure and Axes object, but to be honest, at this point, youā€™re better of to just start making custom plots yourself with matplotlib, Iā€™d say. Stream.plot() is intended as a quick preview but it just canā€™t accommodate every need.

To get you startedā€¦

from obspy import read
import matplotlib.pyplot as plt

st = read()

fig, axes = plt.subplots(nrows=len(st), sharex=True)
for tr, ax in zip(st, axes):
    times = tr.times('matplotlib')
    ax.plot(times, tr.data)
ax.xaxis_date()
fig.autofmt_xdate()
# could als use ObsPyAutoDateFormatter on x Axis
plt.show()

Thanks. Iā€™d already started to look at matplotlib.

Me, at my age, learning a new programming language is not fun.

I really believe you will find it easier to start simple and then add things as you move forward rather then taking that ready made plot and try to poke around in its intestines to make adjustments.

Matplotlib really has good documentation on anything you want to add to your plot. :smiley:

Regarding removing the ā€œlines between subplotsā€, spines is what that is called, so look there for how to do it.
Something like this, probably:

axes[0].spines.bottom.set_visible(False)
axes[-1].spines.top.set_visible(False)
for ax in axes[1:-1]:
    ax.spines.bottom.set_visible(False)
    ax.spines.top.set_visible(False)
from obspy import read
import matplotlib.pyplot as plt 

st = read()

fig, axes = plt.subplots(nrows=len(st), sharex=True)
for tr, ax in zip(st, axes):
    times = tr.times('matplotlib')
    ax.plot(times, tr.data)
ax.xaxis_date()
fig.autofmt_xdate()
# could also use ObsPyAutoDateFormatter on x Axis

plt.subplots_adjust(hspace=0)

axes[0].spines.bottom.set_visible(False)
axes[-1].spines.top.set_visible(False)
for ax in axes[1:-1]:
    ax.spines.bottom.set_visible(False)
    ax.spines.top.set_visible(False)
plt.show()

Many, many thanks. I really appreciate your help.

1 Like

Sorry to be a pain, but Iā€™m really struggling with this - Iā€™ve never done any object-orientated programming.

I can save the figure with plt.savefig( ā€˜save.pngā€™ ) but how do I set the image size?

Iā€™m looking through the documentation, but itā€™s confusing me.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html

figsize=(12, 8) in that plt.subplots() call :+1:

In addition you can specify dpi=... in the savefig() call

1 Like