# How to color line segments according z-value?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832>\
**Category:** Q&A\
**Tags:** gmt\
**Created:** [April 19, 2022, 4:58pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832 "2022-04-19T16:58:41Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 19, 2022, 4:58pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/1 "2022-04-19T16:58:42Z")

</div>

I try to plot aircraft tracks. I have a bunch of coordinates with altitude information and I want to color the segments between the points according to their altitude. See the superbly executed illustration below.

 ![track](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/021ec1669a5aa0d17eacd0f31673ac99aa9045e6.jpeg)

I’m aware of `-Z` but as I understand it this doesn’t really help in my case.

My idea is to pass `plot` a CPT and have it color the line segments accordingly. As every data point might have a different altitude associated with it I want to use the lower value of two consecutive points for choosing the color. To make things worse, in a different context I’m interested in the higher value.

Is there a way to achieve this with pure GMT?

Thank you and all the best,  
Kristof

* * *

Something to play with:

[track\_sample\_data.txt](https://forum.generic-mapping-tools.org/uploads/short-url/bMu2kJdMftswNJGbqqIBqLDQJEL.txt) (19.6 KB)

In the sample data the altitude information (z-value) is in column 3 (the first column being column 0).

```
#!/usr/bin/env bash

gmt begin track_sample
  gmt plot \
    -R007:24:13.64/49:01:59.90/009:54:13.64/51:01:59.90r \
    -JL008:34:13.64/50:01:59.90/49:31:59.90/50:31:59.90/20c \
    track_sample_data.txt -i7,6 -W0.7p,red
gmt end

```

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 19, 2022, 5:13pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/2 "2022-04-19T17:13:06Z")

</div>

Isn’t it a job for categorical CPT ?

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 19, 2022, 5:53pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/3 "2022-04-19T17:53:47Z")

</div>

Hi @PlanetGus, thank you for taking the time to answer. A categorical CPT isn’t really needed. I’m spanning values from ~37.000ft almost all the way to 0ft. I just want to qualitatively show the altitude in a 2D top view.

My problem is how to get this z-value out of the data.

In the end the idea is to highlight areas where aircraft regularly come close to terrain. But that will be a second step to convert the altitudes into height above terrain.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 19, 2022, 7:15pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/4 "2022-04-19T19:15:20Z")

</div>

I think this is a job for **gmt convert -Fv**. I see your input file has text before numerical columns so maybe need awk to select lon,lat,altitude but if you have that then the above command will turn records of lon,lat,alt into records with lon1,lat1,alt1,lon2,lat2,alt2 and you can use that with plot -Sv1p+s -W1p+cl -Cyour.cpt -i0,1,3,4,2|5 depending if you want the first or second point to set the color.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 19, 2022, 7:20pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/5 "2022-04-19T19:20:24Z")

</div>

Actually, this seems to work:

`gmt convert track_sample_data.txt -i7,6,3 -Fv -o0,1,3,4,2`

and you can change the last 2 to 5 (assuming I used the right column for altitude). Pipe through plot as shown earlier

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 19, 2022, 8:05pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/6 "2022-04-19T20:05:34Z")

</div>

I tried something with `-S~` , but I was not able to color it properly. `-Sc` seems to work but I reckon it’s not ideal :

```auto
gmt begin test png
    gmt makecpt -T0/20000/1000 -H > mycpt.cpt
    gmt plot -R007:24:13.64/49:01:59.90/009:54:13.64/51:01:59.90r -JL008:34:13.64/50:01:59.90/49:31:59.90/50:31:59.90/20c track_sample_data.txt -i7,6,3 -Sc0.25c -Cmycpt.cpt -Bafg
gmt end show

```

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 8:07am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/7 "2022-04-21T08:07:32Z")

</div>

Thank you @pwessel! `gmt convert` and `-Sv+s -W+cl` did the trick. Great idea converting the segments to vectors.

 ![track_sample](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/4/4173fbdc720bc30efc82c68460f1ce9ff8211649.png)

[track\_sample\_data.txt](https://forum.generic-mapping-tools.org/uploads/short-url/bMu2kJdMftswNJGbqqIBqLDQJEL.txt) (19.6 KB)

```
#!/usr/bin/env bash

gmt makecpt -T0/20000 -Cseis > track.cpt
gmt convert track_sample_data.txt -i7,6,3 -Fv -o0,1,3,4,2 > trackv.txt

gmt begin track_sample
  gmt plot \
    -R007:24:13.64/49:01:59.90/009:54:13.64/51:01:59.90r \
    -JL008:34:13.64/50:01:59.90/49:31:59.90/50:31:59.90/20c \
    trackv.txt -Sv1p+s -W2p+cl -Ctrack.cpt -i0,1,4,2,3
gmt end 

```

Just some eye-candy with more tracks:

 ![track_sample_multi](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/0e14f8a403760445b0dbe4b87fae9bea787a880d.png)

The idea @PlanetGus brought up with just plotting symbols and colouring them via z-value is quite clever as well. While not ideal in this case it is a useful trick to know. Thank you for that!

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 8:22am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/8 "2022-04-21T08:22:33Z")

</div>

Quick question for @pwessel, @PlanetGus and the other devs:

Is this useful enough to qualify for becoming an example? Besides plotting aircraft tracks it might also be useful for users tracking animals like migrating birds or whales. Could also be used for visualising speeds in racing or similar.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 21, 2022, 8:31am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/9 "2022-04-21T08:31:15Z")

</div>

I’m not a dev, but I like your script 😃  
I definitely would adapt it to flow speed (ice, river … ) when grdimage is not an option.  
Moreover, the script is already there, it might as well be copied in showcase 😉

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 8:37am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/10 "2022-04-21T08:37:58Z")

</div>

Glad you like it! I feel that it is too simple for the showcase. I thought of this one being more of a quick and dirty example how to color-code the z-value into a plot.

In my eyes the showcase should be reserved for beautiful and complicated visualisations showing off what can be accomplished with GMT. A line with a color gradient isn’t that flashy in this context …

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 21, 2022, 8:41am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/11 "2022-04-21T08:41:37Z")

</div>

I think it’s worth to be there, but if you feel like it’s not enough, turn it into a movie with (x,y) plane on the left and (t,z) plane on the right , call it “Flight tracking to avoid tragic collision” and tadam ! Sensational science 😃

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 8:53am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/12 "2022-04-21T08:53:03Z")

</div>

Actually the “line with a color gradient” is a small step in a bigger project where I try to locate potential hotspots where flights in controlled airspace and flights in the adjacent uncontrolled airspace regularly come close to each other. Not in a sensational “barely avoided tragic collision” sense but more as way to raise situational awareness amongst the pilots. This might qualify for the showcase when finished. So far I spent half of last night fighting with `gmtselect` and `gmtmath`.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 21, 2022, 8:56am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/13 "2022-04-21T08:56:32Z")

</div>

What I like about showcase category, is not necessarily to see fancy stuff, but also to see how versatile GMT could be, how people are using it in different fields 🙂

> I spent half of last night fighting

Ah ! overnight projects 😃 I like these unhealthy ones 😃

For hotspots, maybe have a look at “how to create a heatmap” in … showcase 😲 (also the reply from PWessel on how to improve it down the thread.

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 9:08am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/14 "2022-04-21T09:08:45Z")

</div>

That heat map idea is great! I smell feature creep in my project goals … 😉

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 21, 2022, 9:18am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/15 "2022-04-21T09:18:57Z")

</div>

I think we are fine having almost anything in the showcase. The alternative is simple that it wont have many examples, so go for it!

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [April 21, 2022, 10:25am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/16 "2022-04-21T10:25:01Z")

</div>

It’s really great! I feel this example belongs to the “how-to” category as mentioned in [Proposal: Creating a separate GMT tutorial page](https://flint.soest.hawaii.edu/t/proposal-creating-a-separate-gmt-tutorial-page/2831) or [https://www.generic-mapping-tools.org/gmt-examples/](https://www.generic-mapping-tools.org/gmt-examples/).

---

<div class="post-metadata">

**Author:** ![KristofKoch](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/kristofkoch/32/314_2.png) [@KristofKoch](https://forum.generic-mapping-tools.org/u/KristofKoch)\
**Post date:** [April 21, 2022, 12:52pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/17 "2022-04-21T12:52:26Z")

</div>

I agree, that would be a proper place for it 👍

---

<div class="post-metadata">

**Author:** ![maxrjones](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/maxrjones/32/2698_2.png) [@maxrjones](https://forum.generic-mapping-tools.org/u/maxrjones)\
**Post date:** [April 27, 2022, 11:21pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/18 "2022-04-27T23:21:59Z")

</div>

Great to see your new forum posts @KristofKoch! Hope you’re well 🙂

I agree that it would make a great example 👍

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 9, 2022, 5:14pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-line-segments-according-z-value/2832/19 "2022-05-09T17:14:31Z")

</div>

Same fig but this times as seen from Kristof’s cockpit.

```auto
julia> D = gmtread("track_sample_data.txt", i="7,6,3");
julia> plot3d(D, lc=:gradient, lt=[1,5], par=(PS_LINE_CAP="round",), show=true)

```

 ![GMTjl_tmp-fs8](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/c/c8cde04e296b5cf14079ebfc8a8ffe0fdd8afa2e.png)
