1

I was using obspy to plot seismogram, but when you want to customize a little the plot you must use matplotlib. But this way I don't succeed to have a x axis using date time, with a first label display the all date, then only seconds (or %M:%S eventually).

Here the plot using obspy : enter image description here

See the date time axe.

And here my plot using matplotlib :

enter image description here

How to obtain same time axe than previous graphe ?

And finally the code for the matplotlib plot :

trz = st[2].copy()
tr=trz.trim(starttime=t0, endtime=(t0+float(dur)), nearest_sample=True)
tr.filter("bandpass", freqmin=float(flow), freqmax=float(fhigh), corners=2, zerophase=True)
t = np.arange(0, tr.stats.npts / tr.stats.sampling_rate, tr.stats.delta)

label=(tr.stats.network + '.' + tr.stats.station + '.' + tr.stats.location + '.' + tr.stats.channel)

fig, ax = plt.subplots(1,1,figsize=(18,10), dpi=200)
ax.plot(t,tr.data, 'r', label=label)
ax.set_title('Évènement du '+str(date)+'T'+str(hr)+' station : '+str(sta) + '_' + tr.stats.channel)
ax.xaxis.grid(True, which='major', color='g', linestyle='dotted', linewidth=0.3)
ax.yaxis.grid(True, which='major', color='g', linestyle='dotted', linewidth=0.3)
ax.legend(loc='upper left', ncol=4)
ax.set_xlim(tr.stats.starttime,tr.stats.endtime)

formatter = mdates.DateFormatter("%H-%M-%S")
ax.xaxis.set_major_formatter(formatter)
locator = mdates.HourLocator()
ax.xaxis.set_major_locator(locator)
BenjiBoy
  • 141
  • 7
  • Have you tried ax.set_xlabels([...])? This allows you to use the labels you put on this list instead of the numbers – DPM Jun 08 '22 at 12:25
  • yeah but this is not the good format, here it is : array([0.000e+00, 1.000e-02, 2.000e-02, ..., 1.498e+01, 1.499e+01, 1.500e+01]) – BenjiBoy Jun 08 '22 at 13:03
  • It can have strings as values in the list – DPM Jun 08 '22 at 13:03
  • not sure that I'm understanding : can you give me a example ? – BenjiBoy Jun 08 '22 at 13:06
  • Sorry for the confusion. Matplotlib is assigning integers to the x label, instead you can pass your own, so you can get those times and dates and do ax.set_xlabels(["11:59:00", "12:30:10", etc]). You dont have to do this manually of course – DPM Jun 08 '22 at 13:09
  • Ok interesting, I'll try – BenjiBoy Jun 08 '22 at 13:10

1 Answers1

1

Using the set_xlabel() could be a solution (thanks DPM) but it seem that the trace axis is not sinchronized with the data.

Finally using xlim solve the date format (?), then using matplotlib datesformatter to custom interval.

ax.set_xlim((pd.to_datetime(str(tr.stats.starttime))),(pd.to_datetime(str(tr.stats.endtime))))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
ax.xaxis.set_major_locator(mdates.SecondLocator())

And the result :

enter image description here

But, I just can't figure how to have the first label on the date that using the all format (%Y-%m-%dT%H:%M:%S), but maybe that is not so important ...

BenjiBoy
  • 141
  • 7
  • Can't you modify the first label to be what you want? – DPM Jun 08 '22 at 14:37
  • Well, that is precisely what I'm looking for but can't figure it out. Don't know how to target the very first label, to change it's format. – BenjiBoy Jun 08 '22 at 14:59
  • If you already have the list of labels then you can access the first element say labels[0] and set it equal to what you want – DPM Jun 08 '22 at 15:23
  • "labels" variable in my code is just the label of each plot, to fill the legend function after. The labels below xticks are generated via "xaxis.set_major_formatter" and I don't know how to call them. I'm using Spyder and I can't see it in the variable explorer neither. – BenjiBoy Jun 09 '22 at 06:24