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):
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
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 :
Just worth being aware of it, is all.
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: