[obspy-users] overlay plotting or traces

Tobias Megies megies at geophysik.uni-muenchen.de
Fri Mar 14 14:53:21 CET 2014


Yes, for all custom plotting needs beyond a simple preview, directly
using matplotlib is the way to go.

Of course you should consider the possibility of differing start times
of the traces.. simply starting at 0 for each one might not plot
correctly aligned. also, always taking the sampling rate of the first
trace is not failsafe.

To ensure all traces have the same start time you could trim with
filling masked values.. e.g.:

st.trim(starttime=t0, pad=True, fill_value=None)

Furthermore, there's a convenience routine to get relative sample times
in a Trace, so you could do something like:

Here's an example with a modified stream with differing sampling rates
and starttimes to show what I mean:

#############
from obspy import read
import matplotlib.pyplot as plt

# put together modified example stream
st = read()
for i, tr in enumerate(st):
    tr.trim(starttime=tr.stats.starttime + 2 * i)
st[1].resample(200, no_filter=True)

# plot correctly
t0 = min([tr.stats.starttime for tr in st])
st.trim(starttime=t0, pad=True, fill_value=None)

for tr, color in zip(st, ["black", "red", "blue"]):
    times = tr.times()
    plt.plot(times, tr.data, linestyle="-", marker=None,
             color=color, linewidth=1.5, label=tr.id)

plt.grid()
plt.legend()
plt.show()
#############

Or you could also simply use matplotlib's timestamp oriented plotting
and use absolute times of samples to plot, which always plots x axis
correctly no matter what start times / sampling rates etc... e.g.:

#############
from obspy import read
import matplotlib.pyplot as plt

# put together modified example stream
st = read()
for i, tr in enumerate(st):
    tr.trim(starttime=tr.stats.starttime + 2 * i)
st[1].resample(200, no_filter=True)

# plot correctly
for tr, color in zip(st, ["black", "red", "blue"]):
    # get absolute time stamps for every sample
    times = [(tr.stats.starttime + t).datetime for t in tr.times()]
    plt.plot_date(times, tr.data, linestyle="-", marker=None,
                  color=color, linewidth=1.5, label=tr.id)

plt.grid()
plt.legend()
plt.show()
#############


best,
Tobias


On 03/14/2014 02:22 PM, Dirk Rößler wrote:
> Here comes the answer to my own questions:
> 
> 
> figure()
> for i in range(cnt):
>     t=np.linspace(0,st[i].stats.endtime-st[i].stats.starttime,
> (st[i].stats.endtime-st[i].stats.starttime+st[0].stats.delta)*st[0].stats.sampling_rate,endpoint=True)
> 
>     plt.plot(t,st[i].data)
> 
> 
> Dirk
> 
> On 14/03/14 13:51, Dirk Rößler wrote:
>> Ist there a way to plot all traces of a stream in one subplot with same
>> / different colours / symbols?
>>
>> Regards,
>> Dirk
> 
> 
> _______________________________________________
> obspy-users mailing list
> obspy-users at lists.swapbytes.de
> http://lists.swapbytes.de/mailman/listinfo/obspy-users
> 

-- 
Dipl.-Geophys. Tobias Megies

Geophysikalisches Observatorium
Ludwigshöhe 8
82256 Fürstenfeldbruck

Ludwig-Maximilians-Universität
Department für Geo- und Umweltwissenschaften
Sektion Geophysik
Theresienstrasse 41/IV
80333 München

Tel: +49 (0) 89 2180-73981
     +49 (0) 89 2180-4326
Mail: tobias.megies at geophysik.uni-muenchen.de


More information about the obspy-users mailing list