Saving plot_trigger figures

This is probably an easy question for those more experienced with obspy and python but I am having some trouble trying to figure out how to save the plot generated by the plot_trigger function without manually saving the plot when it is shown.

Im planning on having a while loop that calls each trace in a stream separately then runs plot_trigger. Id like the save the plots without them being shown so ive set show=False in plot_trigger. There is no outfile option in the plot_trigger command and I am not sure how else to save it. Hoping someone might be able to help me out with this.

Thanks!

You could simply add plt.savefig(‘myfig.png’) after the line where you call the plot trigger function, as “plt” is the name how the matplotlib.pyplot module is imported according to the source code. See http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig for details of savefig.

For example:

for trace in stream:

plot_trigger(trace, cft, thr_on, thr_off, show=False)
plt.savefig("%s.%s.png" % (trace.stats.station, trace.stats.channel))

You're right, the plot_trigger() function should probably return the
created figure.. but with a bit of matplotlib you can easily grab the
last figure created and work around this obspy API mishap.. try this:

import matplotlib.pyplot as plt
for ... in ...:
    plot_trigger(..., show=False)
    fig = plt.gcf()
    fig.savefig(...)
    plt.close(fig)

cheers!
T

Ah.. didn't see you already replied but your message was held up by list
moderation, sorry Juan!

@Michael if you have long-running loops make sure to close figures after
saving them, otherwise your RAM might run full at some point..

T