首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有numpy数组和2个标签类型的Python轴日期时间

具有numpy数组和2个标签类型的Python轴日期时间
EN

Stack Overflow用户
提问于 2022-06-08 07:53:38
回答 1查看 183关注 0票数 1

我使用ob间谍来绘制地震记录,但是当您想要定制一些绘图时,您必须使用matplotlib。但通过这种方式,我无法成功地使用日期时间来使用x轴,第一个标签显示所有日期,然后只显示秒(或%M:%S最终)。

在这里,使用ob间谍的情节:

看日期,时间,斧头。

在这里,我使用matplotlib绘制的图:

如何获得与以往相同时间的斧头?

最后是matplotlib图的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-06-08 14:12:41

使用set_xlabel()可能是一种解决方案(谢谢DPM),但是跟踪轴似乎没有与数据同步。

最后,使用xlim解决日期格式(?),然后使用matplotlib数据格式化程序来定制时间间隔。

代码语言:javascript
复制
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())

其结果是:

但是,我不知道如何在使用all格式的日期(%Y-%m-%dT%H:%M:%S)上有第一个标签,但也许这并不重要.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72541989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档