Applying a CPT to vectors in PyGMT

Hi all,

I am trying to apply a custom CPT of colors to vectors with makecpt and plot.

  • When cmap=True, nothing is plotted
  • When cmap=False, I get the correct vectors with the correct magnitudes but not the correct colors
  • Other attempts at implementations with the velo function also failed

In trying to apply the color, it usually either ends up messing up the magnitude and/or directionality of the vectors.

`fig3 = pygmt.Figure()

fig3.coast(
region=region,
projection=“M6i”,
shorelines=[“1/0.5p,black”],
borders=[“1/1p,black”, “2/0.5p,black”],
frame=[“WSen”, “xaf”, “yaf”],
)

cpt = pygmt.makecpt(
cmap=“cyclic”,
series=[-90, 90, 0.25],
)

fig3.plot(
data=fig3_vectors,
style=“v0c+m”,
pen=“0.5p,black”,
cmap=True,
)

fig3.show()`

Thank you

Hi @nredick,

welcome to the GMT forum :slightly_smiling_face:!

Thanks for sharing your code. Please also post the content of the variables region and fig3_vectors.

Actually there are two options for what can be colored:

  • the fill of the vector head
  • the line and outline of the vector

Just played around with some codes from the PyGMT Tutorial at Plotting vectors — PyGMT

import pygmt
import numpy as np


vector_num = 5
radius = 3 - (0.5 * np.arange(0, vector_num))
startdir = np.full(vector_num, 90)
stopdir = 180 + (50 * np.arange(0, vector_num))
color = np.arange(0, vector_num)


# -----------------------------------------------------------------------------
# (I) Color-coding of the vector head
# Quantity for color coding must be in the third coloum of the input data

data = np.column_stack(
    [np.full(vector_num, 0), np.full(vector_num, 0), color, radius, startdir, stopdir]
)

fig = pygmt.Figure()
fig.basemap(region=[-5, 5, -5, 5], projection="X10c", frame="ag")

pygmt.makecpt(cmap="batlow", series=[0, vector_num, 1])
fig.plot(data=data, style="m1c+ea", fill="+z", cmap=True)

fig.show()


# -----------------------------------------------------------------------------
# (II) Color-coding of line and outline of the vector
# Need to loop over all vectors (line)
# See also https://www.pygmt.org/v0.14.2/gallery/lines/line_custom_cpt.html
# Last access: 2025/02/20

data = np.column_stack(
    [np.full(vector_num, 0), np.full(vector_num, 0), radius, startdir, stopdir]
)

fig = pygmt.Figure()
fig.basemap(region=[-5, 5, -5, 5], projection="X10c", frame="ag")

pygmt.makecpt(cmap="batlow", series=[0, vector_num, 1])
for i_vector in range(len(data)):
    fig.plot(
        data=[data[i_vector]], style="m1c+ea", zvalue=color[i_vector], pen="2p,+z", cmap=True,
    )

fig.show()

(I) vector head

(II) line and outline