解决obspy.plot(type=‘section‘)绘制按震中距度数排列报错

1. 利用python的obspy库绘制按震中距排列的波形对比图时如果使用震中距为°排列(即设置dist_degree=True),设置了ev_coord也会报错,报错信息如下:
存在问题:trace.stats中并没有coordinates这个属性(似乎可以自己定义,尚不清楚)。

ValueError: Define latitude/longitude in trace.stats.coordinates

2. 解决方法(这或许是一个折中的方法):
如果SAC数据头段中定义了震中距gcarc的值或dist值,可以赋值给trace.stats.distance,这样在dist_degree=False的情况下也可以画出按震中距排列的波形对比图(单位为°)。如果只给出dist值,可以手动转换成°,乘以1000是因为obspy.plot(type='section')下震中距默认单位为km。

for tr in st:
    tr.stats.distance = tr.stats.sac['gcarc'] * 1000

3.完整代码:

import os
import glob
import obspy
import matplotlib.pyplot as plt
from obspy import Stream

sacfile = sorted(glob.glob('*/*sac'))

# Original mean stacked waveforms
st = Stream()
for data in sacfile:
    st += obspy.read(data)

for tr in st:
    tr.stats.distance = tr.stats.sac['gcarc'] * 1000

fig = plt.figure(figsize=(10,6))
st.plot(type='section',orientation='horizontal',recordlength=1200,linewidth=1.0, show=False, fig=fig)
plt.xlabel('Time/(s)')
plt.ylabel('Distance/(°)')
plt.savefig('Wave_gcarc.png',dpi=300)
# plt.show()

参考问题描述:[obspy-users] plotting record sections: type oftrace.stats.coordinates

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值