I am trying to plot an ObsPy (or any python) seismic focal mechanism in time series.
JavaScript
x
18
18
1
import matplotlib.pyplot as plt
2
import datetime
3
import numpy as np
4
from obspy.imaging.beachball import Beach
5
6
# x = np.arange(0,100,4)
7
x = np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)])
8
y = np.random.randint(100, size=x.shape)
9
10
fig, ax = plt.subplots(figsize=(10,6))
11
ax.plot(x,y)
12
13
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
14
15
bball.set_zorder(1)
16
ax.add_collection(bball)
17
plt.show()
18
When using an integer/float series for x it works fine, but the time series fails here:
JavaScript
1
11
11
1
Traceback (most recent call last):
2
File "./beachballs.py", line 15, in <module>
3
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=5, linewidth=1, alpha=0.85)
4
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
5
colors, p = plotDC(np1, size=size, xy=xy, width=width)
6
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
7
collect.append(xy2patch(Y, X, res, xy))
8
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
9
x = x * res[0] + xy[0]
10
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'
11
The function xy2patch in the source code is trying to scale the time by a float value.
Any ideas? Hack the source? Other packages? Thanks.
Advertisement
Answer
Found it. Convert times to matplotlib float representation.
JavaScript
1
20
20
1
import matplotlib.pyplot as plt
2
import datetime
3
import numpy as np
4
from obspy.imaging.beachball import Beach
5
from matplotlib.dates import date2num
6
7
# x = np.arange(0,100,4)
8
9
x = date2num(np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)]))
10
y = np.random.randint(100, size=x.shape)
11
12
fig, ax = plt.subplots(figsize=(10,6))
13
ax.plot(x,y)
14
15
bball = Beach([70, 90, 80], xy=(x[10],y[10]), width=(.1,15), linewidth=1, alpha=0.85)
16
17
bball.set_zorder(1)
18
ax.add_collection(bball)
19
plt.show()
20