How to pass transparency values for individual lines in psxy?

I have a ‘linefile.txt’ with a series of lines in the format

>
lon1 lat1
lon2 lat2
>
lon3 lat3
lon4 lat4
...

and so on. I wanted these lines to be colored by a third value and a colormap, which I couldn’t get to work by appending a third value after lon1 lat1 for example. I was able to get this working by the following line, in which I added the -Z pointing to a file which has a value for each line segment (single column .txt file):

# lines colored with correlation
gmt psxy -R -J $linefile -O -K -W1p+z -Z$colorfile -Ccorr.cpt >> $out

However now I want to change the transparency of each line based on a fourth value, which I understand I should be able to do by adding a -t flag on this command, without a value associated (manual: If no transparencies are given then we expect to read them from the last numerical column(s).).

The issue is I’m not really sure how the formatting of my ‘linefile.txt’ would work. Where would the transparency value for each segment be placed? This sort of comes back to my initial issue (which I worked around with the -Z command) where I was unable to format the colormap value properly into my line file.

Thanks,
Aaron

Given you want both color and transparency to vary per line segment you would have to make something that looks like this:

> first header -W1p,red@45
lon1 lat1
lon2 lat2
> yet another line -W3p,blue@77
lon3 lat 3
lon4 lat4
....etc

Assuming that works you could post a feature request on GMT to ask if the optional 2nd column in - could be used as variable transparency.

I see what you’re saying; but what if I want it to use a colormap and a z-value to determine the color? Initially I tried having the psxy command not use the W+z and -Z command and simply passing in the third value (for the colormap to use) like so

>
lon1 lat1 z-value (for colormap)
lon2 lat2
>
...

which threw an error that it was expecting a third value on line 3 (the lon2 lat2 line). I tried adding the third column z-value to both lines (lon1 line and lon2 line) but then the script never finished running. So my -Z command addition worked around my inability to figure out how to properly format the linefile to accept more than 2 columns. I can’t really find out in the manuals/documentation how to format my linefile in this regard.

Reading -Z makes it pretty clear, no?

  1. No z-values in the data file, just lon lat.

  2. Make a file with one column that holds the z values for each data row in your data file. E.g., if you have 10 2-point lines in 10 segments then you have 20 points and need 20 values in the file passed to -Z. Row order must match your data.

  3. A line can only have one color so each pair of z-values need to be identical, i.e.,

    3
    3
    8
    8
    4
    4

If you an get that to work we can see if the -Z file could take a 2nd optional column with transparencies.

I had the color part working with -Z. My initial test actually revealed that I only need to input the color for each line once, i.e. only one row in my z-file per line segment (each line segment being two rows in my linefile.txt).

I just assumed that with how the manual worded -t, it needed the transparency values in the file that has the lines:

“If no transparencies are given then we expect to read them from the last numerical column(s).”

I guess my misunderstanding is regarding how exactly all the inputs are formatted into the command. Is -Z essentially treating the file I input with it as additional columns onto my initial command? Regardless, I’ll try a test case with a few transparencies in the second column and see if I can get it working.

The stuff about -t and last columns applies to points, not lines. But see what it does - might unintentionally work.

This is how I would do it, if you can, since it is working:

#!/bin/bash
cat << EOF > t.txt
> first header -W15p,red@45
0	1
1	2
> yet another line -W10p,blue@77
3	2
4	3
EOF
gmt plot -R-1/5/-1/5 -JX10c -B t.txt -pdf map

Thanks for this working solution. I will probably end up doing it this way, it just requires a bit more work of doing the cpt lookup for each line segment externally before I write the t.txt and make my figure.

One last hail mary: is it possible to have my cpt have a varying transparency depending on the value? For example, my cpt goes between 0 and 1. At 0, I want the transparency at 100, and at 1, I want it fully opaque?

Meantime, I implemented what I discussed earlier and it has been merged into GMT master. Have a look at the PR which allows either color, transparency or both to be controlled via that -Zfile.

1 Like

Wow, I hadn’t expected you to add this functionality! Thank you so much - seriously. GMT is the best.

1 Like