# waveform plot

**URL:** <https://discourse.obspy.org/t/waveform-plot/545>\
**Category:** Users Mailing List Archive\
**Tags:** archive\
**Created:** [August 21, 2016, 5:51pm UTC](https://discourse.obspy.org/t/waveform-plot/545 "2016-08-21T17:51:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VICIC\_BLAZ\_PHD090001](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@VICIC\_BLAZ\_PHD090001](https://discourse.obspy.org/u/VICIC_BLAZ_PHD090001)\
**Post date:** [August 21, 2016, 5:51pm UTC](https://discourse.obspy.org/t/waveform-plot/545/1 "2016-08-21T17:51:57Z")

</div>

Dear all,

I am trying to plot simple figure of waveforms (not so simple that I could just use plot() )

Here is the code I have:

tstart = UTCDateTime(detection.detect\_time - 5.0)  
tend = UTCDateTime(detection.detect\_time + 20.0)

st1 = sta.copy()  
st1a = st1.slice(starttime=tstart, endtime=tend)  
st1det = st1a.detrend(type=‘constant’)

#waveforms  
st1deta = st1det.copy()  
st1filta = st1deta.filter(‘bandpass’, freqmin=fminb, freqmax=fmaxb, zerophase=True)

#time vector  
t = np.arange(0, st1a[0].stats.npts / st1a[0].stats.sampling\_rate, st1a[0].stats.delta)

#setup figure  
fig = plt.figure(figsize=(20,40))  
ax1a = fig.add\_axes([0.07, 0.92, 0.9, 0.075])

#plot wfs  
ax1a.plot(t, st1filta[0], ‘k’)  
#ax1a.axvline(detection.detect\_time, color=‘k’, linestyle=‘–’)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Wehling-Be](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@Sebastian\_Wehling-Be](https://discourse.obspy.org/u/Sebastian_Wehling-Be)\
**Post date:** [August 22, 2016, 10:44am UTC](https://discourse.obspy.org/t/waveform-plot/545/2 "2016-08-22T10:44:29Z")

</div>

Hello Blaz,

I think you just have to subtract tstart from detection.detect\_time to get the relative time which is 5.0 in your case.

ax1a.axvline(detection.detect\_time - tstart, color=‘k’, linestyle='–‚)

or

ax1a.axvline(5.0, color=‘k’, linestyle=‘—’)

I hope that helps.

Cheers,

Sebastian

---

<div class="post-metadata">

**Author:** ![megies](https://discourse.obspy.org/user_avatar/discourse.obspy.org/megies/32/9_2.png) [@megies](https://discourse.obspy.org/u/megies)\
**Post date:** [August 22, 2016, 10:48am UTC](https://discourse.obspy.org/t/waveform-plot/545/3 "2016-08-22T10:48:23Z")

</div>

You are doing a relative-type plot in seconds starting at 0 (the  
starttime of the trace in your case), so you also need to convert your  
absolute time you want to plot into that relative timeframe to match  
your "time vector". In your failed attempt you had the waveform plot at  
very low x values (-5 to 20, I think) and the vertical line at a high  
value (something like 1e9 -- the float representation of the UTCDateTime  
object, i.e. the POSIX timestamp).

You need to do something like..

> > > ax1a.axvline(detection.detect\_time - st1a[0].stats.starttime, ...)

..to get the relative timing of you detection time. Although, in your  
example code the cut out time window is hard coded anyway, so your  
vertical line should be at 5 seconds in the given example.

Please also note that you can use a convenience method to get the "time  
vector":

> > > ax1a.plot(st1filta[0].times(), st1filta[0], 'k')

Also see some related convenience changes in current master that will  
enter 1.1 once it's released (not very soon), especially if at some  
point you want absolute times on the x axis:

> <https://github.com/obspy/obspy/pull/1307>
>
> First and foremost intended for convenient plotting in matplotlib axes with a
> ti…me axis..
> 
> \`\`\` python
> import matplotlib.pyplot as plt
> from obspy import read
> 
> tr = read()\[0\]
> plt.plot(tr.times(type="matplotlib"), tr.data)
> fig = plt.gcf()
> fig.autofmt\_xdate()
> fig.axes\[0\].xaxis\_date()
> plt.show()
> \`\`\`
> 
> Still needs ..
> \- \[x\] tests
> \- \[x\] mention in "plotting waveforms" tutorial page
> \- \[x\] changelog

> <https://github.com/obspy/obspy/pull/1339>
>
> ..for plotting on matplotlib time axes.
> 
> This adds a method \`UTCDateTime.to\_matp…lotlib()\` and a property \`UTCDateTime.mpl\` for even shorter notation.

T

---

<div class="post-metadata">

**Author:** ![VICIC\_BLAZ\_PHD090001](https://discourse.obspy.org/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@VICIC\_BLAZ\_PHD090001](https://discourse.obspy.org/u/VICIC_BLAZ_PHD090001)\
**Post date:** [August 22, 2016, 1:42pm UTC](https://discourse.obspy.org/t/waveform-plot/545/4 "2016-08-22T13:42:24Z")

</div>

Thank you!

It works without problem.

Blaž
