Change beach ball color based on depth

Hello! I want to define the color of my beach balls according to the depth they have (and also to show that on the colorbar). How do I do it? There’s no a “fill” parameter as with figure.plot(). Thanks!

Here’s the plot part my code so far:

fig = pygmt.Figure()

# Create a colormap for depth values

pygmt.makecpt(cmap="jet", series=[df['depth'].min(), df['depth'].max()])

# Plot the topography and coordinate grid

fig.grdimage('@earth_relief_01m', region=region, projection='D0/-70/-90/-49/12c', cmap='geo')

fig.basemap(region=region, projection='D-0/70/-90/-49/12c', frame=True)

#Plot focal mechanisms

fig.meca(

    spec=df,

    scale='0.5c',

    projection='D0/-70/-90/-49/12c',

    region=region,

    cmap=True

)

# Add colorbar with label "Profundidad focal (km)"

fig.colorbar(frame=["x+lProfundidad focal (km)", "y+lm"])

fig.show()

Hello @jcmefra,

Welcome to the GMT forum :slightly_smiling_face:!

Can you please provide the data you read into the pandas.DataFrame df. Please also report the values of the varaiable region. Currently, it is a bit difficult to figure out what is going wrong in detail.

Hello! The data head is this:

||longitude|latitude|depth|mrr|mtt|mff|mrt|mrf|mtf|exponent|plot_longitude|plot_latitude|
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|0|-71.87|-65.47|23|1.09|4.33|-5.42|0.88|0.55|4.08|24|-71.87|-65.47|
|1|-25.64|-59.99|39|0.70|0.14|-0.83|-0.25|0.28|-0.41|24|-25.64|-59.99|
|2|28.11|-52.43|22|-0.52|2.89|-2.37|0.61|-0.06|3.21|23|28.11|-52.43|
|3|-128.84|-55.35|19|-0.39|2.53|-2.14|0.03|0.62|3.10|24|-128.84|-55.35|
|4|-24.97|-58.03|56|0.67|0.19|-0.86|0.00|-0.02|0.25|24|-24.97|-58.03|

Region is this:

region = [-180, 180, -90, -50]

I’m being able to plot the beach balls but they are all black and white, I want to change the compressionfill color according to the depth.

Is it possible? I was able to do it with figure.plot(). Thanks!

Yes it is possible to color-code beachballs based on their hypocentral depths.

I feel the issue comes from having two colormaps:

  • one for the elevation of the grid: grdimage with “geo”
  • one for the hypocentral depth of the beachball: meca or makecpt with “jet”

You have to create the colormap for the hypocentral depth after calling grdimage. Otherwise, cmap=True used in meca will consider the colormap set up by grdimage.

Input txt file: meca_mt.txt (424 Bytes)

import pygmt
import pandas as pd


# Read txt file into Dataframe
df = pd.read_csv("meca_mt.txt", delimiter="\t")

# Define stuy area
region = [-180, 180, -90, -50]

# Create figure object
fig = pygmt.Figure()

# Plot the topography and coordinate grid
# Please note, I reduced the resolution from "01m" to "10m" to make the code run faster
fig.grdimage('@earth_relief_10m', region=region, projection='D0/-70/-90/-49/12c', cmap='geo')

fig.basemap(region=region, projection='D-0/70/-90/-49/12c', frame=True)

# Create a colormap for depth values
# Create the colormap for the hypocentral depth after "grdimage"
# Otherwise "cmap=True" considers the colormap (here "geo") set up by "grdimage"
pygmt.makecpt(cmap="jet", series=[df['depth'].min(), df['depth'].max()])

# Plot focal mechanisms
fig.meca(
    spec=df,
    scale='0.5c',
    projection='D0/-70/-90/-49/12c',
    region=region,
    cmap=True,
)

# Add colorbar with label "Profundidad focal (km)"
fig.colorbar(frame=["x+lProfundidad focal (km)", "y+lm"])

fig.show()
# fig.savefig(fname="meca_two_cpts.png")

Output figure:

2 Likes

@yvonnefroehlich, almost all of your replies should be put in a showcase gallery… just sayin’

1 Like

Thank you so much! I will update my code with your suggestions and come back if I’ve got any additional inquiry.