5

Is there a way to plot transparent data points when using palette?

I currently have the following code:

set style fill transparent solid 0.2 noborder
set palette rgb 22,13,-22
plot 'mydata.dat' u 1:2:3 ps 0.3 palette

My feeling is that transparency is overwritten by the arguments of the plot command.

wpm
  • 77
  • 1
  • 8
  • 1
    `set style fill transparent` applies to fill areas, like the facets making up a pm3d surface or the interior of a polygon or the `filledcurves` plot style. It does not apply to lines and points. Your plot command appears to use points since you give a pointsize option, although the set of commands is incomplete so I am not 100% certain. – Ethan Feb 16 '20 at 20:35
  • Does this answer your question? [Gnuplot: How to make scatter plots with transparent points](https://stackoverflow.com/questions/34532568/gnuplot-how-to-make-scatter-plots-with-transparent-points) – Friedrich -- Слава Україні Feb 17 '20 at 09:15
  • Thanks @Friedrich. It doesn't answer the question because I cannot see an easy way to use palette at the same time. Still useful! – wpm Mar 05 '20 at 12:18
  • Ah! I also need this: a palette where all colors have some level of transparency! It's rare to hit something missing in gnuplot. – daruma Dec 16 '21 at 07:00
  • I just asked for this gnuplot feature on sourceforge, – daruma Dec 16 '21 at 07:11

2 Answers2

7

Is there a way to plot transparent data points when using palette?

If you check help palette you will not find (or I overlooked) a statement about transparency in the palette. It looks like you can set the palette in different ways for RGB, but not for ARGB (A=alpha channel for transparency). So, I assume it is not possible with palette to have transparency (please correct me if I am wrong). As workaround you have to set your transparency "manually" by setting the color with some transparency. You can find the formulae behind the palettes by typing show palette rgbformulae.

The following examples creates a plot with random point positions in xrange[0:1] and yrange[0:1] and random points size (from 2 to 6) and random transparency (from 0x00 to 0xff). The color is determined by x according to your "manual palette". I hope you can adapt this example to your needs.

Code:

### "manual" palette with transparency
reset session

# These are the rgb formulae behind palette 22,13,-22
set angle degrees
r(x) = 3*x-1 < 0 ? 0: (3*x-1 > 1) ? 1 : 3*x-1
g(x) = sin(180*x)
b(x) = 1-(3*x-1) < 0 ? 0: (1-(3*x-1) > 1) ? 1 : 1-(3*x-1)

set xrange [0:1]
set yrange[-0.1:1.1]

RandomSize(n) = rand(0)*4+2              # random size from 2 to 6
RandomTransp(n) = int(rand(0)*0xff)<<24  # random transparency from 0x00 to 0xff
myColor(x) = (int(r(x)*0xff)<<16) + (int(g(x)*0xff)<<8) + int(b(x)*0xff) + RandomTransp(0)

set samples 200
plot '+' u (x=rand(0)):(rand(0)):(RandomSize(0)):(myColor(x)) w p pt 7 ps var lc rgb var not

### end of code

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Works for me. Thanks also for the background information. – wpm Mar 05 '20 at 12:21
  • A follow-up comment: When printing `myColor(x)` this sometimes results in negative values. This is probably because at calculation or at printing level this results in a signed integer. Seems to work fine, but maybe @theozh could help clarify? – wpm Mar 06 '20 at 13:16
  • I don't know how exactly you are using it. In my case `x` in `myColor(x)` is only for `0<=x<=1`. There should not be negative values with the formulae `r(x),g(x),b(x)` given above. – theozh Mar 06 '20 at 14:05
  • If I print a few sample values, e.g., repeatedly execute `print(myColor(0.5))` I get negative values. Gnuplot 5.2 patchlevel 2. It seems to work and I assume it's a conversion between signed and unsigned under the hood, but I thought I'd mention. – wpm Mar 06 '20 at 16:24
  • 1
    apparently, gnuplot is interpreting it right as unsigned. Just for checking, try: `do for [i=1:100] { print sprintf("%08x % 11u", myColor(0.5), myColor(0.5))}`, which will print 100 random colors and if you format them as `%u`, they are all positive ;-). Btw, in the gnuplot documentation I can't find format specifier `%u` in the list. – theozh Mar 07 '20 at 07:16
3

New answer for Dev version 5.5

The new function set colormap allows to define a transparent palette. First, one defines the fully opaque palette in the usual way, then creates a copy of it and adds transparency to all points:

set palette rgb 22,13,-22
set colormap new MYPALETTE
transparency = 0.5
do for [i=1:|MYPALETTE|] {MYPALETTE[i] = MYPALETTE[i] + (int(transparency*0xff)<<24)}
func(x,y) = x*y
splot func(x,y) w pm3d fillcolor palette MYPALETTE

enter image description here

Of course, this will also work for points, the command in your case will be plot 'mydata.dat' u 1:2:3 ps 0.3 lc palette MYPALETTE

Eldrad
  • 733
  • 3
  • 15