# Controlling the data --\> color gradient mapping in Plots

**URL:** https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096
**Category:** Visualization
**Tags:** question, plotting
**Created:** [January 25, 2019, 3:54pm UTC](https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096 "2019-01-25T15:54:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [January 25, 2019, 3:55pm UTC](https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096/1 "2019-01-25T15:55:00Z")

</div>

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a717b59705fd944533320b57b54a0f2dedaf798.png)

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:

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

```

Voila:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/4/a47d5e968d37a852eec254f4873292c070c77b4f.png)

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

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [January 25, 2019, 6:10pm UTC](https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096/2 "2019-01-25T18:10:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [January 25, 2019, 7:11pm UTC](https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096/3 "2019-01-25T19:11:13Z")

</div>

> [@tobydriscoll](#):
>
> 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)

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

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

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b579bdbc1b2089a8cba58458009c489e3fd7081.png)

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.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 25, 2019, 7:31pm UTC](https://discourse.julialang.org/t/controlling-the-data-color-gradient-mapping-in-plots/20096/4 "2019-01-25T19:31:01Z")

</div>

Xlim works as a tuple too?
