2

What does gnuplot need to plot the line of a function** using the palette linecolor i.e. as a gradient? (I tried terminal wxt 0 enhanced and svg, but will try more)? The documentation says palette is "used to color pm3d surfaces, heat maps, and other plot elements."

**POST-ANSWER NOTE No. 1: the function type will matter significantly - e.g. set parametric is quite different from, e.g, sin(x) - vide infra.

A number of Stack Overflow posts show how to use gnuplot to plot data points from an input data file as a gradient, using palette as the linecolor - these work as described in my hands and in the wxt and svg terminals (not shown - I will try some other ones).

I tried plotting sin(x) as described :

reset
unset key
set palette defined ( 0 "blue", 1 "red" )
plot sin(x) lw 3 lc palette z

I can see the gradient bar/key on the right, going from -10 to 10 (red), but the line appears to be at zero on the scale (in this case) - neither -10 (blue) or 10 (red) (see image):

blue-to-red palette gradient with sin function

as an aside : I thought set key off should keep the key from appearing on the plot? seems not (this apparently uses the default palette): (spoiler: to turn off the color scale/key bar on the right: unset colorbox - see comment that follows answer below.)

reset
set key off
plot sin(x) lw 3 lc palette z

default palette - key off not working I guess

An example of what I am looking for - a function plotted using a line that continuously changes color as displayed on the graph - is found in this SO post about matlab.

To conclude : is something missing from the gnuplot commands above to get a function like sin(x) plotted with a gradient - or perhaps recurring gradient, so it starts over again?

programs/environment used:

gnuplot 5.4 patchlevel 2

Ubuntu 22.04

POST-ANSWER NOTE No. 2: It is important to take care with the plot commands. If w l is omitted, as in this script:

### plot with lines with palette
### directly from the answer except omitting w l
reset session
unset key
set palette defined ( 0 "blue", 1 "red" )

# plot command to show plus signs:
plot '+' u 1:(sin(x)):(sin(x)) lc palette z
#
# original answer plot commands:
#plot '+' u 1:(sin(x)):(sin(x)) w l lw 3 lc palette z
### end of script

... literal plus signs + will be plotted : demonstration of plus signs being plotted.

Just worth being aware of it, is all.

1 Answers1

2

If you want to use palette z for variable line color you have to give a z-column, e.g. plot ... (x):(y):(z).... This doesn't work directly if you are using functions.

But you have the special filename + (check help special-filenames) which is similar but where you can give columns, e.g.

plot '+' u 1:(sin($1)):(sin($1)) w lines palette z

Instead of $1 you could also use x.

Script:

### plot with lines with palette
reset session
unset key
set palette defined ( 0 "blue", 1 "red" )

plot '+' u 1:(sin(x)):(sin(x)) w l lw 3 lc palette z
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Amazing! I'd really like to use this in parametric equations, should it work? I tried taking the answer from [this](https://stackoverflow.com/questions/74828702/gnuplot-parametric-equation-positive-variable-ok-negative-variable-confusing) post - and in particular, I made the change at line 15: ```plot '+' u x(t),y(t) w l lw 2 lc palette z ``` but get ```undefined variable: t```. – Bryan-StackExchange Mar 16 '23 at 19:23
  • 1
    @Bryan-StackExchange well, you can check `help parametric`, but I guess you have the same issue: where is your "z-column"? You can do everything with `'+'`, e.g. `plot [0:1] '+' u (x($1)):(y($1)):(z($1)) w l lc palette z`. – theozh Mar 16 '23 at 19:37
  • I see - I have a dilemma - post another question on that specific case - parametric equations colored with ```palette``` - or add a short _post_-_facto_ note to this question - because it seems important, what kind of equation - parametric, etc. Perhaps I could do both. Answer is good, I think, either way. – Bryan-StackExchange Mar 16 '23 at 19:44
  • and can the key/scale be turned off. – Bryan-StackExchange Mar 16 '23 at 19:57
  • 1
    @Bryan-StackExchange `unset colorbox`. Check `help colorbox`. – theozh Mar 16 '23 at 20:12
  • this might be for a whole other question on special filenames in gnuplot : how to adjust the pointsize of the ```+```? I didn't even realize it is literally a ```+``` being plotted, so I started looking into terminal types, the characters available, and trying e.g. ```pt 2``` in the plot command - and it seems that the ```'+'```, ```'++```unadjustable - so tricks like ```'.'```, ```{\267}```, or ```'\.'```` are not going to work. .. a practical problem was gnuplot generating an enormous plot file size because of the number of ```+```. – Bryan-StackExchange Mar 21 '23 at 21:29
  • 1
    @Bryan-StackExchange yes, that's a completely different question. The special-filename `'+'` has nothing to do with the pointtype (`pt 1` is the symbol `+`). Check `help special-filenames`. About the size check `help pointsize` and `help with`. – theozh Mar 22 '23 at 07:48
  • I understand, yet it is interesting that a literal ```+``` is literally plotted on the output graph - at least in certain cases. I'll have to look into it. – Bryan-StackExchange Mar 22 '23 at 11:04
  • 1
    @Bryan-StackExchange type `test` in the gnuplot console and you will see the different pointtype symbols. If you want to plot, e.g. the character `A` as point use `plot .... w p pt "A"`. Check `help points`. – theozh Mar 22 '23 at 11:10
  • a brief note: after the ```plot``` command, ```w l lw 3``` indeed produces nice lines. however, one might try, for instance, ```lw 3``` - this will produce a series of literal plus signs - e.g. ```+```, following the function. so for the script above, use ```plot '+' u 1:(sin(x)):(sin(x)) lw 3 lc palette z``` to see this. Perhaps this is bad form or not - but worth knowing and seeing. – Bryan-StackExchange Mar 23 '23 at 16:03