# Specify range of gradient when plotting lines

**URL:** https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385
**Category:** Visualization
**Tags:** question, colors, plots
**Created:** [April 23, 2024, 10:42am UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385 "2024-04-23T10:42:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 23, 2024, 10:42am UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385/1 "2024-04-23T10:42:59Z")

</div>

Hi all,

I would like to apply a color gradient to lines based on their slopes. The problem I am having is that the line with the smallest slope is not visible. I tried changing the the values argument for `cgrad`, but I can’t seem to constrain the range so that the lines are visible. Any help would be appreciated.

**MWE**

```julia
using Plots
linear(x, β0, β1) = β0 + β1 * x
x = range(-2, 2, length=50)
β1s = range(0, 2, length=5)
ys = map(β1 -> linear.(x, 0, β1), β1s)
plot(x, ys, line_z = β1s', colorbar=false, leg=:topleft, color = cgrad(:Purples, .0:.1:.4))

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 23, 2024, 11:54am UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385/2 "2024-04-23T11:54:23Z")

</div>

Pick two named purple colors [from here](https://juliagraphics.github.io/Colors.jl/stable/namedcolors/) for the end points, and pass it to:

```julia
color = cgrad([:lavender, :purple4])

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 23, 2024, 11:57am UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385/3 "2024-04-23T11:57:46Z")

</div>

> [@rafael.guerra](#):
>
> `[:lavender, :purple4]`

Thank you! I was just about to explore that possibility when I was looking through some examples where a vector of colors was passed. Good to know that is a solution!

Do you know whether passing 0:.1:.4 to `cgrad` should have constrained the gradient, or am I misunderstanding its function?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 23, 2024, 12:04pm UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385/4 "2024-04-23T12:04:32Z")

</div>

I don’t know, I’ve never seen anything like that.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 23, 2024, 12:06pm UTC](https://discourse.julialang.org/t/specify-range-of-gradient-when-plotting-lines/113385/5 "2024-04-23T12:06:12Z")

</div>

Ok. I will mark your answer as the solution. If someone thinks cgrad has bug, please reply.

Thanks again!
