# Color gradient by factor in StatsPlots

**URL:** <https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 4, 2019, 4:53pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704 "2019-11-04T16:53:29Z")\
**Posts on this page:** 10\
**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:** [November 4, 2019, 4:53pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/1 "2019-11-04T16:53:29Z")

</div>

Hi all-

I would like to use a gradient to color code lines based on values of a factor. In the example below, I would like the two groups of lines to have the same colors based on the slopes. How do I do that?

```julia
using DataFrames, StatsPlots, Distributions 
x = 1:10
linear(b0, b1, x) = b0 + b1*x
B0 = [0,10]
B1 = range(.5, stop=1, length=5)
y = [linear.(b0, b1, x) for b1 in B1 for b0 in B0]
y = vcat(y...)
df = DataFrame(group = repeat(0:1,inner=50), slope = repeat(repeat(B1, inner=10), outer =2),
    x = repeat(x,outer=10), y = y)
@df df plot(:x, :y, group = (:group,:slope), legend=false, palette = cgrad(:viridis,:slope))

```

![temp](https://global.discourse-cdn.com/julialang/original/3X/d/6/d67e3fd105b1ab5f62438348299922ee798104eb.png)

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [August 16, 2021, 12:23pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/2 "2021-08-16T12:23:41Z")

</div>

Did you figure it out? I’m trying to do something similar with using a color map on the groups…

---

<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:** [August 16, 2021, 11:02pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/3 "2021-08-16T23:02:32Z")

</div>

@hdavid16, unfortunately I did not find a solution to this problem. I am still interested in a solution if someone else has one.

---

<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:** [August 16, 2021, 11:21pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/4 "2021-08-16T23:21:31Z")

</div>

One more laborious way, replaces the last one-liner above (_not working as expected_) by:

```julia
# (continuation):
XX, YY, SL = zeros(10,10), zeros(10,10), zeros(10)
i = 1
for d in groupby(df,[:group,:slope])
    XX[:,i], YY[:,i], SL[i] = d.x, d.y, d.slope[1]
    i += 1
end
I = sortperm(SL)
plot(legend=false, ratio=1, xlims=extrema(x),cbar=true)
plot!(XX[I,:], YY[I,:], line_z = SL[I]')

```

![Plot_grouped_lines_colored_by_line_z](https://global.discourse-cdn.com/julialang/original/3X/9/8/983b9325a8f13ea9820c73c675e31bbf68dd905f.png)

---

<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:** [August 16, 2021, 11:56pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/5 "2021-08-16T23:56:59Z")

</div>

Thanks! Do you suppose there is a bug in StatsPlots or was I doing something incorrectly in my original post?

---

<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:** [August 17, 2021, 12:04am UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/6 "2021-08-17T00:04:37Z")

</div>

Sorry, don’t know those recipes enough to confirm it.

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 17, 2021, 12:25am UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/7 "2021-08-17T00:25:07Z")

</div>

OP was just before getting a solution.

Minimal change solution:

```julia
using DataFrames, StatsPlots, Distributions 
x = 1:10
linear(b0, b1, x) = b0 + b1*x
B0 = [0, 10]
B1 = range(.5, stop=1, length=5)
y = [linear.(b0, b1, x) for b0 in B0 for b1 in B1] # `b1 in B1` and `b0 in B0` exchanged
y = vcat(y...)
df = DataFrame(group = repeat(0:1,inner=50), slope = repeat(repeat(B1, inner=10), outer =2),
    x = repeat(x,outer=10), y = y)
@df df plot(:x, :y, group = (:group,:slope), legend=false,
    color = cgrad(:viridis,:slope), line_z = :slope) # palette -> color, `line_z = :slope` added

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d79bd17b46794f452080c70ac01c83538645e7f.jpeg)

---

<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:** [August 17, 2021, 12:30am UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/8 "2021-08-17T00:30:07Z")

</div>

Thank you @genkuroki! I had no idea that I should pass `line_z = :slope` as an argument.

---

<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:** [August 17, 2021, 6:36am UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/9 "2021-08-17T06:36:39Z")

</div>

Lol, this is embarrassing as this was one of the first things tried.  
Checked the REPL history file `repl_history.jl` just to be sure:

```julia
# time: 2021-08-16 15:52:31 GTB Daylight Time
# mode: julia
	    @df df plot(:x, :y, group = (:group,:slope), line_z=:slope, legend=false, palette = cgrad(:viridis,:slope))

```

Somehow the colors looked scrambled but cannot reproduce again. Nevermind…

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [August 17, 2021, 1:02pm UTC](https://discourse.julialang.org/t/color-gradient-by-factor-in-statsplots/30704/10 "2021-08-17T13:02:08Z")

</div>

The response on slack is useful too: [Slack](https://julialang.slack.com/archives/C8JKM4JQ6/p1629150686005400)
