Pygmt colorful line plotting

Dear all,

I have a question regarding plotting colormap-colored lines in PyGMT.

Normally, I plot profile data as symbols colored by a colormap using style and cmap=True, for example:

fig.plot(
data=summary_n[[“p”, “median”, “median”]],
style=“c0.2c”,
cmap=True
)

This works as expected, but it produces colored points, whereas I would like a continuous line colored by the same colormap.

I tried switching to a line plot using a z column and pen="+z":

xyz = summary_n[[“p”, “median”]].copy()
xyz[“z”] = summary_n[“median”]

fig.plot(
data=xyz,
pen=“2p,+z”,
cmap=True
)

However, this still produces a uniform black line, instead of a line colored by the colormap.

Is it currently possible in PyGMT to draw a **continuous line whose color varies along the line according to a colormap?

Cheers,
MN

Hi @muhammetnergizci,

it looks like you have to use a loop to color lines using a colormap; please see this gallery example https://www.pygmt.org/v0.18.0/gallery/lines/line_custom_cpt.html.

To apply specific colors to the lines one can set up a multi-segment file.

test_data.txt (160 Bytes)

> -Wbrown # line 1
-5 1  # x11 y11
5 1   # x12 y12
> -Wblue # line 2
-5 0  # x21 y21
5 0   # x22 y22
> -Wred  # line 3
-5 2  # x31 y31
5 2   # x32 y32
import pygmt

fig = pygmt.Figure()
fig.basemap(region=[-5, 5, -1, 3], projection="X10c/4c", frame=1)

fig.plot(data="test_data.txt", pen="5p")

fig.show()

That is not very practical to do. From memory only, in Julia (see example https://www.generic-mapping-tools.org/GMTjl_doc/examples/plotting_functions/50_plot3.html) what I do is to sample the line at some increment and plot vectors with no heads. Vector bodies are colored (and thickened) using a colormap.