Applying Shapefile Attributes to gmt psxy


I’ve been trying to apply attributes from a shapefile in GMT when using the psxy command.

To be specific, I am trying to vary the color and width of lines based on an attribute in the shapefile. I am also trying to control the color and radius of points based on a shapefile attribute.

A minimum working example is provided below (code and output, example data is attached as a zip). I have had some success and been able to control the color of the lines based on the information on the shapefile. However I have been unsuccessful in trying to extract and apply the attribute in the shapefile to set the line widths. For the points I have been entirely unsuccessful :frowning:


# minimum working example of lines and points from shapefiles

proj=-JM0/5i  # projection
reg=-RUS  # region

lines=lines.shp  # shapefile with lines
circles=circles.shp  # shapefile with circles

line_thickness=thickness  # line thickness attribute name
circle_size=size  # circle size attribute name

ofile=ex  # output file name

# make colormap and plot the lines
gmt makecpt -Cabyss -T0.0/1.0/0.25 -D > colors.cpt
gmt psxy $lines $reg $proj -W1.5+cl -aZ=$line_thickness -Ccolors.cpt -K > $

# plot the circles
gmt psxy $circles $reg $proj -Sc0.25 -O >> $

# conversion from eps to pdf +m<top/bottom/left/right> I think for margins
gmt psconvert $ -Tf -A+m0.25/0.5/0.25/0.25  # to pdf
gmt psconvert $ -Tg -A+m0.25/0.5/0.25/0.25  # to png
rm -f $
rm -f $ofile.eps


This example shows how the “line_thickness” attribute from the “lines.shp” file is successfully extracted and applied to color the psxy lines based on a colormap that has been defined using makecpt. (8.8 KB)

I think you need to look a bit more at the docs for -a to see how you can affect pen widths. As for circle sizes: If you give a fixed size in -Sc0.25 then that is what you will get of course. Give -Sc if you wish to pick up the size from the data file via -aZ=$circle_size.

See what W, G etc stand for in -a.

@pwessel thank you for the reply. In hindsight I am realizing my initial post did not make clear what variations on -Sc and -a that I have previously tried. I will provide some of these attempts below and the corresponding behavior I am seeing. I did look through the -a flag documentation and the examples, but was unable to find a clear example of how to do what it is I am trying to do. So apologies for the lack of clarity, and thanks again for your suggestions and tips.

For the lines I have tried the set the line thickness by attribute in the following ways:

  1. Tried to use the -aW command to set the pen thickness from the attribute, however this results in 3 black lines of the default pen thickness

    gmt psxy $lines $reg $proj -aW=$line_thickness

  2. Tried to simply omit the width in the pen command as you’d suggested for the circles with the below line. This colors the lines by attribute but again their widths are default pen widths.

    gmt psxy $lines $reg $proj -W+cl -aZ=$line_thickness -Ccolors.cpt

  3. Lastly I tried to append +gLINE to -aW to tell GMT that this is a line I am trying to plot. This resulted in an error: psxy [ERROR]: Cannot read OGR/GMT files when -a is used to define output format. This error suggests to me that I cannot use +g with these shapefiles.

    gmt psxy $lines $reg $proj -aW=$line_thickness+gLINE

For the circles I have tried to set their fill color and size in the following ways:

  1. Started with your suggestion of omitting the 0.25 circle size and using the -aZ command to vary the circle size by attribute:

    gmt psxy $circles $reg $proj -Sc -aZ=$circle_size -Ccolors.cpt

This gave me the warning: psxy [WARNING]: Mismatch between actual (2) and expected (3) fields near line 10 in file circles.shp and the plot was not produced.

  1. Next I tried holding the circle size and just varying the fill color using -aG:

    gmt psxy $circles $reg $proj -Sc0.25 -aG=$circle_size -Ccolors.cpt

This produced the same warning as my attempt to vary circle size.

It is unclear to me how to resolve either my issue with the lines or the circles.

For the lines, I am not sure how to configure psxy so that it applies the attribute values as pen thicknesses, despite having resolved this for the line color itself. Would it be possible to use the same attribute or multiple to gain control of both line color and thickness by attribute?

For the circles, I am not sure what GMT is expecting and why this error is being thrown. My only guess is that to set the color, 3 RGB values are expected? However this is inconsistent with the way I was able to color the lines, also using psxy, so this is what is confusing to me. Of course the end-goal is similar, I would like to have control over both the circle size and color, preferable both simultaneously using a shapefile attribute.

Thanks again for any help or suggestions, and I apologize for the lack of clarity in my original question.


1 Like

Thanks for your careful explanation, Jay. It is likely we have some limitations on how this works, especially for setting pen thickness. I will use your examples to improve the woefully lacking documentation and determine what is and is not currently doable, hopefully this week. Note that if you want to have variable circle size and color via z, then your input needs to be x, y, z, size and you need more than one column assignment via -a and possibly -i (whether it works or not…).

1 Like