0

I'm using obspy to display seismogram but also, I'm ploting directly with matplotlib because I want to customize my plots. Here is my code :

st.trim(starttime=t0, endtime=(t0+float(dur)), nearest_sample=True)
tr3 = st.copy()
#tr3=tr3.trim(starttime=t0, endtime=(t0+float(dur)), nearest_sample=True)
tr3.filter("bandpass", freqmin=float(flow), freqmax=float(fhigh), corners=4, zerophase=True)

label = {}
x = 0
for x in range(3):
       
    globals()[f"label{x}"] = (tr3[x].stats.network + '.' + tr3[x].stats.station + '.' + tr3[x].stats.location + '.' + tr3[x].stats.channel)

fig, ax = plt.subplots(3,1,sharex=False,sharey=False,figsize=(18,10), dpi=200)
    
y = 0
for y in range(3):
    print(y)
    
    ax[y].plot(tr3[y].times(("utcdatetime")),tr3[y].data, 'r', label=globals()[f"label{y}"])
    ax[y].xaxis.grid(True, which='major', color='k', linestyle='dotted', linewidth=0.5)
    ax[y].yaxis.grid(True, which='major', color='k', linestyle='dotted', linewidth=0.5)
    ax[y].legend(loc='upper left', ncol=4)
    limin = pd.to_datetime(str(tr3[y].stats.starttime))
    limax = pd.to_datetime(str(tr3[y].stats.endtime))
    ax[y].set_xlim(limin,limax)
    ax[y].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    ax[y].xaxis.set_major_locator(mdates.SecondLocator())
    
fig.suptitle('Évènement du '+str(date)+'T'+str(hr)+' station : '+str(sta))

In the line

fig, ax = plt.subplots(3,1,sharex=False,sharey=False,figsize=(18,10), dpi=200)

When I use "sharex=True", the second and third plot disappear, because of the xlim parameter I think. Just don't understand why.

But sharex=True could be usefull for me, this way I don't need to hiding x axis on the first and second plot. Am I missing something ?

BenjiBoy
  • 141
  • 7
  • It seems that you are setting the x limits three times, once for every subplot. That doesn't really make sense in the context of having a shared x-axis. It seems better to me to have one overall x minimum and x maximum, then set the xlimits outside of the loop for the first axis, and the rest should scale accordingly. – 9769953 Jun 10 '22 at 06:58
  • Hello, I tried to put ax[0].set_xliam(limin,limax) before and after the loop (and comment the one inside), and use sharex=True again : doesn't work neither. – BenjiBoy Jun 13 '22 at 14:11

0 Answers0