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

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.

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

And then create a legend file like this:

#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…

#!/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


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:

#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
#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


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.

I think z=value only works for assigning numerical values, 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.

Maybe setting up a colorbar and using -L to get separate rectangles can work as an alternative approach here.

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

I think this is NOT a CPT.

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

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

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