# Display lines handle with variable color inside legend in Makie

**URL:** <https://discourse.julialang.org/t/display-lines-handle-with-variable-color-inside-legend-in-makie/91447>\
**Category:** Visualization\
**Tags:** plotting, makie\
**Created:** [December 8, 2022, 9:46pm UTC](https://discourse.julialang.org/t/display-lines-handle-with-variable-color-inside-legend-in-makie/91447 "2022-12-08T21:46:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [December 8, 2022, 9:46pm UTC](https://discourse.julialang.org/t/display-lines-handle-with-variable-color-inside-legend-in-makie/91447/1 "2022-12-08T21:46:30Z")

</div>

```julia
using CairoMakie

fig = Figure(resolution=(600,400))
ax = Axis(fig[1, 1])

xs = 0:0.01:10
ys = 0.5 .* sin.(xs)

lines!(ax, xs, ys, label="uniform color")
lines!(ax, xs, ys .+ 1, color=ys.^2, label="variable color")
axislegend(ax)

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c572fa1dda705c5cb6d65fd14a0094cafa57a56.png)

Is it possible to show a colored line with the color gradient for the line handle inside the legend? Right now it is shown as a black line, which doesn’t match the color of the top line.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 9, 2022, 9:33am UTC](https://discourse.julialang.org/t/display-lines-handle-with-variable-color-inside-legend-in-makie/91447/2 "2022-12-09T09:33:44Z")

</div>

There’s no quick builtin way to do this, because usually varying colors are combined with a Colorbar.  
You can just pass multiple points and colors to a `LineElement` because it just passes those to the underlying `lines` call without checking if they’re scalar, but you can’t set the colormap that way:

```julia
f, ax, li = lines(range(0, 10, 100), sin, color = 1:100)
le = LineElement(
    points = Point2f.(range(0, 1, 10), 0.5 .+ 0.5 * sin.(range(0, 2pi, 10))),
    color = 1:10,
)
Legend(f[1, 2], [le], ["hi"])
f

```

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

To change the colormap, you currently either have to make your own `LineElement`-like type where that is possible or reach into the internals. This code can break at any time:

```julia
f, ax, li = lines(range(0, 10, 100), sin, color = 1:100, colormap = :inferno)
le = LineElement(
    points = Point2f.(range(0, 1, 10), 0.5 .+ 0.5 * sin.(range(0, 2pi, 10))),
    color = 1:10,
)
l = Legend(f[1, 2], [le], ["hi"])
l.blockscene.children[1].plots[2].colormap = :inferno
f

```

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