ObsPy 在时间序列中绘制沙滩球
ObsPy Plot Beachball in Time Series
我正在尝试按时间序列绘制 ObsPy(或任何 python)地震震源机制。
import matplotlib.pyplot as plt
import datetime
import numpy as np
from obspy.imaging.beachball import Beach
# x = np.arange(0,100,4)
x = np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)])
y = np.random.randint(100, size=x.shape)
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x,y)
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
bball.set_zorder(1)
ax.add_collection(bball)
plt.show()
当对 x 使用 integer/float 系列时,它工作正常,但时间系列在这里失败:
Traceback (most recent call last):
File "./beachballs.py", line 15, in <module>
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 119, in Beach
colors, p = plotDC(np1, size=size, xy=xy, width=width)
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 645, in plotDC
collect.append(xy2patch(Y, X, res, xy))
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 656, in xy2patch
x = x * res[0] + xy[0]
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'
source code 中的函数 xy2patch 正在尝试按浮点值缩放时间。
有什么想法吗?破解源代码?其他包裹?谢谢
找到了。将时间转换为 matplotlib 浮点表示。
import matplotlib.pyplot as plt
import datetime
import numpy as np
from obspy.imaging.beachball import Beach
from matplotlib.dates import date2num
# x = np.arange(0,100,4)
x = date2num(np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)]))
y = np.random.randint(100, size=x.shape)
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x,y)
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=(.1,15), linewidth=1, alpha=0.85)
bball.set_zorder(1)
ax.add_collection(bball)
plt.show()
感谢 Aleksander 在正确方向上的推动。
PS。我想给对这个问题投反对票的人投反对票,因为他没有解释。
我正在尝试按时间序列绘制 ObsPy(或任何 python)地震震源机制。
import matplotlib.pyplot as plt
import datetime
import numpy as np
from obspy.imaging.beachball import Beach
# x = np.arange(0,100,4)
x = np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)])
y = np.random.randint(100, size=x.shape)
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x,y)
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
bball.set_zorder(1)
ax.add_collection(bball)
plt.show()
当对 x 使用 integer/float 系列时,它工作正常,但时间系列在这里失败:
Traceback (most recent call last):
File "./beachballs.py", line 15, in <module>
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 119, in Beach
colors, p = plotDC(np1, size=size, xy=xy, width=width)
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 645, in plotDC
collect.append(xy2patch(Y, X, res, xy))
File "/usr/local/lib/python3.3/site-packages/obspy-0.9.2_787_gf176-py3.3-linux-x86_64.egg/obspy/imaging/beachball.py", line 656, in xy2patch
x = x * res[0] + xy[0]
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'
source code 中的函数 xy2patch 正在尝试按浮点值缩放时间。
有什么想法吗?破解源代码?其他包裹?谢谢
找到了。将时间转换为 matplotlib 浮点表示。
import matplotlib.pyplot as plt
import datetime
import numpy as np
from obspy.imaging.beachball import Beach
from matplotlib.dates import date2num
# x = np.arange(0,100,4)
x = date2num(np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)]))
y = np.random.randint(100, size=x.shape)
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x,y)
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=(.1,15), linewidth=1, alpha=0.85)
bball.set_zorder(1)
ax.add_collection(bball)
plt.show()
感谢 Aleksander 在正确方向上的推动。
PS。我想给对这个问题投反对票的人投反对票,因为他没有解释。