# From GR.colormap to Plots.cgrad

**URL:** <https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920>\
**Category:** General Usage\
**Created:** [February 20, 2020, 5:28pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920 "2020-02-20T17:28:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [February 20, 2020, 5:28pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/1 "2020-02-20T17:28:03Z")

</div>

I would like to use the `bone` color map.  
As the `clibraries()` does not have it, but GR has as it as a colormap, tried various permutations of:

```julia
GR.setcolormap(15) # this works
Plots.plot(xy, c=cgrad(GR.colormap()) # this fails

```

`eltype(GR.colormap())` is `Float64` and `size(Gr.colormap())` is `(256, 3)`

Any suggestions on how I could get there?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [February 23, 2020, 4:51pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/2 "2020-02-23T16:51:46Z")

</div>

This works for me (although this is probably not the most straightforward solution):

```julia
import GR
using Plots
GR.setcolormap(15)
cmap15 = cgrad([RGB(r...) for r=eachrow(GR.colormap())])
Plots.heatmap(rand(10,10), color=cmap15)

```

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [February 24, 2020, 8:43am UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/3 "2020-02-24T08:43:52Z")

</div>

Thanks a lot. That’s good enough for me right now.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [February 24, 2020, 10:16am UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/4 "2020-02-24T10:16:35Z")

</div>

There’s also

```julia
using ColorSchemes
Plots.heatmap(rand(10,10), seriescolor = cgrad(ColorSchemes.bone.colors))
Plots.heatmap(rand(10,10), seriescolor = cgrad(ColorSchemes.bone_1.colors))

```

By the way - I don’t know why there are two similarly named color schemes here.

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 9, 2020, 1:27pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/5 "2020-03-09T13:27:43Z")

</div>

This works great and straight forward. I now saved the GR color gradient to a file and can load them with JLD. Not having to load GR and Plots in the same global scope makes my plotting much more readable. Now one more issue:  
**How can I reverse the ColorGradient?**  
In Plots.jl, the ColorGradients can be revsersed with appending `_r`.  
I figured this is done internally by the `cgrad_reverse` function in `PlotUtils` `color_gradients.jl`  
How could I call this function?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [March 9, 2020, 4:00pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/6 "2020-03-09T16:00:01Z")

</div>

`Plots.PlotUtils.cgrad_reverse` takes a symbol as argument only.

You could do something like this:

```julia
cmap15 = GR.colormap() |> eachrow |> c->(r->RGB(r...)).(c) |> cgrad
cmap15_r = GR.colormap() |> eachrow |> c->(r->RGB(r...)).(c) |> reverse |> cgrad

```

or

```julia
cmap15 = ColorSchemes.bone.colors |> cgrad
cmap15_r = ColorSchemes.bone.colors |> reverse |> cgrad

```

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 27, 2020, 4:14pm UTC](https://discourse.julialang.org/t/from-gr-colormap-to-plots-cgrad/34920/7 "2020-03-27T16:14:39Z")

</div>

First of: the `ColorSchemes.bone.colors` is not the color scheme I am looking for. To me it just doesn’t appear as nice.  
I still struggle a bit in understanding `ColorGradients`.  
I did not manage to reverse a `ColorGradient` as such. `eachrow` can not iterate through it.  
My solution is now once:

```julia
using PlotUtils
using FileIO
using GR

GR.setcolormap(15)
cmap15 = cgrad([RGB(r...) for r=eachrow(GR.colormap())])
cmap15_r = cgrad([RGB(r...) for r=eachrow(GR.colormap()[end:-1:1,:])])

save(File(format"JLD","/home/joengel/lib/julia/utils/cmaps.jld"),"cmap15",cmap15,"cmap15_r",cmap15_r)

```

And for plotting

```julia
using FileIO
cmap15 = load("~/lib/julia/utils/cmaps.jld")["cmap15"]
cmap15_r = load("~/lib/julia/utils/cmaps.jld")["cmap15_r"]

```
