Skip to content

Commit 99f74e0

Browse files
seismanweiji14
andauthored
Figure.meca: Fix beachball offsetting with dict/pandas inputs (#2202)
Co-authored-by: Wei Ji <[email protected]>
1 parent 3e33467 commit 99f74e0

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

pygmt/src/meca.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ def meca(
189189
Depth(s) of event location in kilometers. Must be the same length as
190190
the number of events. Will override the ``depth`` values in ``spec``
191191
if ``spec`` is a dict or pd.DataFrame.
192-
plot_longitude: int, float, list, or 1d numpy array
192+
plot_longitude: int, float, str, list, or 1d numpy array
193193
Longitude(s) at which to place beachball. Must be the same length as
194194
the number of events. Will override the ``plot_longitude`` values in
195195
``spec`` if ``spec`` is a dict or pd.DataFrame.
196-
plot_latitude: int, float, list, or 1d numpy array
196+
plot_latitude: int, float, str, list, or 1d numpy array
197197
Latitude(s) at which to place beachball. List must be the same length
198198
as the number of events. Will override the ``plot_latitude`` values in
199199
``spec`` if ``spec`` is a dict or pd.DataFrame.
@@ -272,10 +272,10 @@ def meca(
272272
spec["latitude"] = np.atleast_1d(latitude)
273273
if depth is not None:
274274
spec["depth"] = np.atleast_1d(depth)
275-
if plot_longitude is not None: # must be in string type
276-
spec["plot_longitude"] = np.atleast_1d(plot_longitude).astype(str)
277-
if plot_latitude is not None: # must be in string type
278-
spec["plot_latitude"] = np.atleast_1d(plot_latitude).astype(str)
275+
if plot_longitude is not None:
276+
spec["plot_longitude"] = np.atleast_1d(plot_longitude)
277+
if plot_latitude is not None:
278+
spec["plot_latitude"] = np.atleast_1d(plot_latitude)
279279
if event_name is not None:
280280
spec["event_name"] = np.atleast_1d(event_name).astype(str)
281281

@@ -293,9 +293,13 @@ def meca(
293293
newcols = ["longitude", "latitude", "depth"] + param_conventions[convention]
294294
if "plot_longitude" in spec.columns and "plot_latitude" in spec.columns:
295295
newcols += ["plot_longitude", "plot_latitude"]
296+
spec[["plot_longitude", "plot_latitude"]] = spec[
297+
["plot_longitude", "plot_latitude"]
298+
].astype(str)
296299
kwargs["A"] = True
297300
if "event_name" in spec.columns:
298301
newcols += ["event_name"]
302+
spec["event_name"] = spec["event_name"].astype(str)
299303
# reorder columns in DataFrame
300304
spec = spec.reindex(newcols, axis=1)
301305
elif isinstance(spec, np.ndarray) and spec.ndim == 1:

pygmt/tests/test_meca.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,34 @@ def test_meca_dict_offset():
253253
return fig
254254

255255

256+
@pytest.mark.mpl_image_compare(filename="test_meca_dict_offset.png")
257+
def test_meca_dict_offset_in_dict():
258+
"""
259+
Test offsetting beachballs for a dict input with offset parameters in the
260+
dict.
261+
262+
See https://github.com/GenericMappingTools/pygmt/issues/2016.
263+
"""
264+
fig = Figure()
265+
focal_mechanism = dict(
266+
strike=330,
267+
dip=30,
268+
rake=90,
269+
magnitude=3,
270+
plot_longitude=-124.5,
271+
plot_latitude=47.5,
272+
)
273+
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
274+
fig.meca(
275+
spec=focal_mechanism,
276+
scale="1c",
277+
longitude=-124,
278+
latitude=48,
279+
depth=12.0,
280+
)
281+
return fig
282+
283+
256284
@pytest.mark.mpl_image_compare
257285
def test_meca_dict_eventname():
258286
"""

0 commit comments

Comments
 (0)