# In case of categorical cpt, z=value is not applied to the fill of the legend symbol

**URL:** <https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103>\
**Category:** Q&A\
**Tags:** gmt, cpt, legend, color\
**Created:** [July 18, 2024, 7:47am UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103 "2024-07-18T07:47:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tllc46](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tllc46](https://forum.generic-mapping-tools.org/u/tllc46)\
**Post date:** [July 18, 2024, 7:47am UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103/1 "2024-07-18T07:47:01Z")

</div>

Hello gmt users, I posted this question because I didn’t get the expected results while using gmt’s legend module. According to the legend module manual, the symbol’s fill can be specified by cpt and z=value format. However, in the case of categorical cpt, this method failed.

First, prepare the following categorical cpt.

```auto
#cpt_file.cpt
H red
He orange
Li yellow
Be green
B blue
C purple

```

And then create a legend file like this:

```auto
#legend_file
A cpt_file.cpt
S - c 0.3c z=H black - Hydrogen
S - c 0.3c z=He black - Helium
S - c 0.3c z=Li black - Lithium
S - c 0.3c z=Be black - Berylium
S - c 0.3c z=B black - Boron
S - c 0.3c z=C black - Carbon

```

If I run a simple script like this…

```auto
#!/bin/bash

gmt begin gmt_result png
        gmt basemap -R0/1/0/1 -JX5c -B
        gmt legend legend_file -DjRM+w3c+o0.1c -F+p
gmt end show

```

 ![gmt_result](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/f/fd6ba54ded16f7c1f36e77dac52a9096c5a72ba4.png)  
As shown in the figure above, only the first fill is correct, but all other fills come out the same as the first fill.

A regular cpt like this doesn’t cause any problems:

```auto
#cpt_file.cpt
1 red 2 orange
2 orange 3 yellow
3 yellow 4 green
4 green 5 blue
5 blue 6 purple
6 purple 7 purple

```

```auto
#legend_file
A cpt_file.cpt
S - c 0.3c z=1 black - One
S - c 0.3c z=2 black - Two
S - c 0.3c z=3 black - Three
S - c 0.3c z=4 black - Four
S - c 0.3c z=5 black - Five
S - c 0.3c z=6 black - Six

```

 ![gmt_result](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/7/71b64a961e9658dca5faebb0e22c481ac71f38e7.png)  
I’m wondering if this is really a bug, or if it’s explained in the manual but I couldn’t find it. My operating system is Ubuntu 22.04 LTS, gmt version is 6.3, and I installed it via apt.

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [July 18, 2024, 11:24am UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103/2 "2024-07-18T11:24:54Z")

</div>

> [@tllc46](#):
>
> According to the legend module manual, the symbol’s fill can be specified by cpt and z=value format. However, in the case of categorical cpt, this method failed.

I think `z=value` only works for assigning numerical `value`s, not string literals like `z=Li`  
(same true for `ogr2ogr -zfield`)

I would also appreciate if `z=value` and the aspatial field assignment in general (`-a=...` worked for string literals, making text labeling and plotting strings easier with `plot` and `text` gmt modules but this is not the case yet.

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [July 20, 2024, 6:43pm UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103/3 "2024-07-20T18:43:59Z")

</div>

Maybe setting up a colorbar and using [**-L**](https://docs.generic-mapping-tools.org/6.5/colorbar.html#l) to get separate rectangles can work as an alternative approach here.

```bash
gmt begin cat_cpt_cb png

    gmt basemap -R-5/5/-5/5 -JX5c -B
    gmt makecpt -C"green,blue,purple,orange,red,yellow" -T0/5/1 -F"+cBerylium,Boron,Carbon,Helium,Hydrogen,Lithium"
    gmt colorbar -C -DjMR+v+w2c/0.2c -L3p

gmt end show

```

 ![cat_cpt_cb](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/9/93bc97a059a7549faee9d2cc2bbed77ab3ff04a4.png)

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [July 20, 2024, 10:18pm UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103/4 "2024-07-20T22:18:28Z")

</div>

> [@tllc46](#):
>
> First, prepare the following categorical cpt.
> 
> ```auto
> #cpt_file.cpt
> H red
> He orange
> Li yellow
> Be green
> B blue
> C purple
> 
> ```

I think this is NOT a CPT.

If you run Yvonne command you will get this categorical CPT:

```auto
gmt makecpt -C"green,blue,purple,orange,red,yellow" -T0/5/1 -F"+cBerylium,Boron,Carbon,Helium,Hydr
ogen,Lithium"
0 green L ;Berylium
1 blue L ;Boron
2 purple L ;Carbon
3 orange L ;Helium
4 red L ;Hydrogen
5 yellow B ;Lithium
N 128

```

---

<div class="post-metadata">

**Author:** ![tllc46](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tllc46](https://forum.generic-mapping-tools.org/u/tllc46)\
**Post date:** [July 22, 2024, 8:19am UTC](https://forum.generic-mapping-tools.org/t/in-case-of-categorical-cpt-z-value-is-not-applied-to-the-fill-of-the-legend-symbol/5103/5 "2024-07-22T08:19:28Z")

</div>

Thank you so much for your quick reply. It would be nice if this feature could be officially included in GMT later.
