# Using only a part of a colormap in Makie

**URL:** <https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784>\
**Category:** General Usage\
**Tags:** question, plotting, makie, glmakie, colorbar\
**Created:** [November 4, 2022, 5:05pm UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784 "2022-11-04T17:05:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trashspam7](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@trashspam7](https://discourse.julialang.org/u/trashspam7)\
**Post date:** [November 4, 2022, 5:05pm UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784/1 "2022-11-04T17:05:54Z")

</div>

I have a figure where I am plotting a line and want that line to be colored according to some other array. How do I use only part of a colormap? Or even reverse a sub-section of a colormap?

I don’t have a MWE, but the relevant parts of my code are below:

```julia
nodalplot = nothing
for coordsi in coords_list
    global nodalplot
    nodalplot = lines!(ax, coordsi[:, 1], coordsi[:, 2], coordsi[:, 3]; color=freqnodal, linewidth=10, colormap=:brg)
end
Colorbar(fig[1, 2], nodalplot)

```

 ![sg13-nodal](https://global.discourse-cdn.com/julialang/original/3X/6/5/65d5f75badce1ae665aee2cd8757d23d93c5d340.png)

So for example, I only want to use the blue → red section of the `brg` colormap. I can set the `colorrange` while plotting the lines, but it doesn’t allow me to change the limits of the Colorbar.

```julia
nodalplot = lines!(ax, coordsi[:, 1], coordsi[:, 2], coordsi[:, 3]; color=freqnodal, linewidth=10, colormap=:brg, colorrange=(0.315, 0.35))
Colorbar(fig[1, 2], nodalplot)

```

 ![sg13-nodal](https://global.discourse-cdn.com/julialang/original/3X/d/4/d44d976c8949d03e899deb98bc33abd1c98ce553.png)

Trying to pass a `colorrange` arugment into `Colorbar` gives the error:

```julia
ERROR: Both colorrange + limits are set, please only set one, they're aliases. colorrange: (0.315, 0.35), limits: (0.315, 0.35)

```

whereas passing a `limits` argument gives the error:

```julia
ERROR: You should not pass the `limits` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.

```

---

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [November 4, 2022, 5:53pm UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784/2 "2022-11-04T17:53:08Z")

</div>

You can make your own colormap with e.g. `cgrad`, and use it with `colormap=...`. following the documentation:

[https://docs.juliaplots.org/latest/generated/colorschemes/#ColorGradient](https://docs.juliaplots.org/latest/generated/colorschemes/#ColorGradient)

example code:

```julia
using GLMakie
GLMakie.activate!()

cm = cgrad([:blue, :red])

xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

fig = Figure()

ax, hm = heatmap(fig[1, 1], xs, ys, zs, colormap=cm)
Colorbar(fig[1, 2], hm)

fig

```

---

<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:** [November 4, 2022, 8:08pm UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784/3 "2022-11-04T20:08:10Z")

</div>

You can also extract part of an existing colormap, they’re just vectors of colors after all

---

<div class="post-metadata">

**Author:** ![trashspam7](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@trashspam7](https://discourse.julialang.org/u/trashspam7)\
**Post date:** [November 5, 2022, 3:32am UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784/4 "2022-11-05T03:32:33Z")

</div>

How do I extract the colors from an existing colormap?

---

<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:** [November 5, 2022, 6:17am UTC](https://discourse.julialang.org/t/using-only-a-part-of-a-colormap-in-makie/89784/5 "2022-11-05T06:17:52Z")

</div>

By indexing into it, like `Makie.to_colormap(:name)[1:10]`
