# How to pass transparency values for individual lines in psxy?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199>\
**Category:** Q&A\
**Tags:** gmt\
**Created:** [August 24, 2023, 1:05am UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199 "2023-08-24T01:05:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aka](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aka](https://forum.generic-mapping-tools.org/u/aka)\
**Post date:** [August 24, 2023, 1:05am UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/1 "2023-08-24T01:05:09Z")

</div>

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

```auto
>
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):

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

---

<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:** [August 24, 2023, 10:00am UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/2 "2023-08-24T10:00:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aka](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aka](https://forum.generic-mapping-tools.org/u/aka)\
**Post date:** [August 24, 2023, 6:33pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/3 "2023-08-24T18:33:29Z")

</div>

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

```auto
>
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.

---

<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:** [August 25, 2023, 9:28am UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/4 "2023-08-25T09:28:56Z")

</div>

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.,

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

---

<div class="post-metadata">

**Author:** ![aka](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aka](https://forum.generic-mapping-tools.org/u/aka)\
**Post date:** [August 25, 2023, 6:36pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/5 "2023-08-25T18:36:51Z")

</div>

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.

---

<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:** [August 27, 2023, 8:33pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/6 "2023-08-27T20:33:59Z")

</div>

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

---

<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:** [August 27, 2023, 8:44pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/7 "2023-08-27T20:44:31Z")

</div>

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

```

 ![map](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/a/ad71a4c472c57a0abd869a7bd44ca1487f7c379a.png)

---

<div class="post-metadata">

**Author:** ![aka](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aka](https://forum.generic-mapping-tools.org/u/aka)\
**Post date:** [August 28, 2023, 8:55pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/8 "2023-08-28T20:55:43Z")

</div>

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?

---

<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:** [August 29, 2023, 6:35am UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/9 "2023-08-29T06:35:13Z")

</div>

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.

> <https://github.com/GenericMappingTools/gmt/pull/7765>
>
> Currently, \*\*-Z\*\*\_file\_ is a \*\*plot\*\* (and \*\*plot3d\*\*) option used to assign col…ours to line segments or polygons via a CPT(z) lookup using the last column in \_file\_. The number of lines in \_file\_ must match the number of lines or polygons. This PR adds two new modifiers to \*\*-Z\*\*:
> 
> \*\*+t\*\* Instead of modifying the colour, interpret the \_z\_-value as transparency in 0-100% range.
> \*\*+T\*\* Expect the \_two\_ last columns in \_file\_ to have \_transparency\_ and \_z\_-value and change these on a per-segment basis.
> 
> This PR follows from the discussion on the \[forum\](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199).
> 
> Here is a simple example of the four ways to draw lines w/wo color and/or with/without transparency:
> 
> \`\`\`
> \#!/bin/bash
> cat \<\< EOF \> z.txt
> \# \[transparency\] z-value
> 35	2
> 85	8
> EOF
> cat \<\< EOF \> tr.txt
> \# transparency
> 35
> 85
> EOF
> cat \<\< EOF \> t.txt
> \> first header
> 0	1
> 1	2
> \> yet another
> 3	2
> 4	3
> EOF
> gmt begin linetransp png
> gmt makecpt -T0/10 -Cjet
> gmt subplot begin 2x2 -Fs8c -R-1/5/-1/5
> # Constant red lines
> gmt plot -W15p,red t.txt -c
> # Colored lines, no transparency
> gmt plot -W15p+z t.txt -C -Zz.txt -c
> # Transparent lines, no color
> gmt plot -W15p+z t.txt -Ztr.txt+t -c
> # Transparent and colored lines
> gmt plot -W15p+z -Zz.txt+T t.txt -C -c
> gmt subplot end
> gmt end show
> 
> \`\`\`
> 
> !\[linetransp\](https://github.com/GenericMappingTools/gmt/assets/26473567/7a979bb7-472c-48eb-b350-38cee522d6f2)
> 
> Once finalised I will do the same to \*\*plot3d\*\*.

---

<div class="post-metadata">

**Author:** ![aka](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@aka](https://forum.generic-mapping-tools.org/u/aka)\
**Post date:** [August 29, 2023, 6:24pm UTC](https://forum.generic-mapping-tools.org/t/how-to-pass-transparency-values-for-individual-lines-in-psxy/4199/10 "2023-08-29T18:24:08Z")

</div>

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