Modify

Ticket #343 (closed support request: worksforme)

Opened 3 months ago

Last modified 3 months ago

UTC on the x-axis in subplot

Reported by: lupi@… Owned by:
Priority: trivial Component: External
Keywords: UTC Cc:

Description

Hello, mine is really a trivial question but I am not able to find a way out.

I am using plt.subplot as I need to have the waveforms of three different stations (9 traces) in the same diagram. I need to have a diagram with UTC time on the x-axis as I want to show the UTC time when the events occurred.

I am sure that there is a plot() function that I am missing but i do not which (and in case how to use it).

Thanks in advance, Matteo

Attachments

utc Download (1.9 KB) - added by lupi@… 3 months ago.

Change History

Changed 3 months ago by lupi@…

  • attachment utc Download added

comment:1 follow-up: ↓ 2 Changed 3 months ago by barsch

Why not using the internal plot method?

from obspy.core import read
from obspy.core import UTCDateTime

t1 = UTCDateTime("2011-07-22 00:00:00")
t2 = UTCDateTime("2011-07-22 23:59:59")

st1 = read('./SNAE0_2011_203', starttime=t1, endtime=t2)
st2 = read('./SNAE1_2011_203', starttime=t1, endtime=t2)
st3 = read('./SNAE2_2011_203', starttime=t1, endtime=t2)

st = st1 + st2 + st3
st.sort()
st.plot()

comment:2 in reply to: ↑ 1 Changed 3 months ago by barsch

from lupi@...

Hello and thanks for the fast reply. I was using the sub.plot function because I have to plot the filtered data and if I do

vertical_local_SNAE0.filter('bandpass', freqmin=1.0, freqmax=6.0, corners=2, zerophase=True) 
north_local_SNAE0.filter('bandpass', freqmin=1.0, freqmax=6.0, corners=2, zerophase=True) 
east_local_SNAE0.filter('bandpass', freqmin=1.0, freqmax=6.0, corners=2, zerophase=True) 

then I do not know how to plot the filtered results as

st = vertical_local_SNAE0.data + north_local_SNAE0.data + east_local_SNAE0.data

st.sort()
st.plot()

gives me the error

'numpy.ndarray' object has no attribute 'plot'

attached is what I am doing..

Thanks, Matteo

comment:3 Changed 3 months ago by barsch

Hi Matteo,

plot() is a method of a ObsPy Stream or Trace object - so don't call it on the actual data itself which is an NumPy array. So what you want to do is the following:

from obspy.core import read
from obspy.core import UTCDateTime

t1 = UTCDateTime("2011-07-22 00:00:00")
t2 = UTCDateTime("2011-07-22 23:59:59")

# read in the data once - not multiple times !
orig_stream = read('./SNAE0_2011_203', starttime=t1, endtime=t2)

# now you could access those traces on the stream object itself
# >>> vertical_SNAE0 = orig_stream[0]
# but we don't need to go via each trace object, instead we

# make a copy of the original stream in order to work with the whole stream
local_stream = orig_stream.copy()

# filter all components at once - no need to filter each single trace
local_stream.filter('bandpass', freqmin=1.0, freqmax=6.0, corners=2, zerophase=True)

# again you could apply filter also on each single trace, e.g. for the first trace:
# >>> vertical_SNAE0.filter('bandpass', ...)
# or using the stream object directly
# >>> local_stream[0].filter('bandpass', ...)


# in order to use the internal plot() method on both streams in the same plot 
# window we have to give the copied (read filtered) stream another
# id (==network.station.location.channel) by changing e.g. the location code of
# the second stream
for trace in local_stream:
    trace.stats.location = '01'


# now merge both original and filtered stream into a single stream object
stream = orig_stream + local_stream

# sort and plot
stream.sort()
stream.plot()

If the internal plot method is not enough for your project have a look at:  http://docs.obspy.org/tutorial/filtering_seismograms.html.

Hope it helps, Robert

Last edited 3 months ago by barsch (previous) (diff)

comment:4 Changed 3 months ago by barsch

added small bug fix in script above

comment:5 Changed 3 months ago by megies

You can also have a look at  matplotlib.pyplot.plot_date.

If you use the standard matplotlib.pyplot.plot you might want to have a look at  matplotlib.dates.date2num,  Figure.autofmt_xdate(),  Axes.xaxis_date().

You can find examples in the  matplotlib gallery.

best, Tobias

comment:6 Changed 3 months ago by anonymous

Hello and thanks for all the links. However, if I take the script above and I try to run it I get the following message:

TclError?: no display name and no $DISPLAY environment variable WARNING: Failure executing file: <test_robert_good_one.py>

Matteo

comment:7 Changed 3 months ago by barsch

just add the following code in a place that gets read before any other pylab/matplotlib import:

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')

The alternative is to set it in your .matplotlibrc

Last edited 3 months ago by barsch (previous) (diff)

comment:8 Changed 3 months ago by anonymous

I did so and I still get the same error message... no luck/success. Thanks anyway,

Matteo

comment:9 Changed 3 months ago by barsch

what os are you using?

comment:10 Changed 3 months ago by barsch

  • Status changed from new to closed
  • Resolution set to worksforme

no reply for days - closing for now

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.