Controlling the data --> color gradient mapping in Plots

question
plotting

#1
x = (0:80)/80;
y = @.sin(3*exp(x)); 

using Plots
cg = cgrad(:viridis,range(0,stop=1,length=64))
plot(x,y,line_z=y,color=cg)

What I get (this is GR, but other backends seem to be similar):

This is not what I expected. I thought the line_z parameter would be used to index within the color gradient, giving colors like this:

[RGB(cg[y]) for y in y]

Voila:

All the negative values are “clipped” to the deep purple.

It seems that in the plot, the supplied color values are affinely mapped to use the full range of the color gradient. I find that counterintuitive, as I expected the gradient to define a fixed/absolute mapping to color values. I’ve searched unsuccessfully for a keyword or toggle that gives this behavior. What am I missing?

(I find this pretty important behavior for a sequence or animation in which I want colors to have fixed interpretations in time.)

TIA,
Toby


#2

The extrema of the colorbar default to the extrema of the line_z, so if you don’t specify them they’ll vary depending on the plot. You can set them to some constant value with:

plot(x, y, line_z=y, color=:viridis, clims=(0,1))

#3

Thank you for highlighting what was hiding in plain sight. Just to wrap up the thread, what I should have done was

x = (0:80)/80;
y = @.sin(3*exp(x)); 

using Plots
plot(x,y,line_z=y,color=:viridis,clims=(0,1))

It’s unfortunate that we have “xlim”, which is a vector, and “clims”, which is a tuple. But I’ll take yes for an answer.


#4

Xlim works as a tuple too?