From GR.colormap to Plots.cgrad

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:

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?

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

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

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

There’s also

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.

2 Likes

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?

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

You could do something like this:

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

or

cmap15 = ColorSchemes.bone.colors |> cgrad
cmap15_r = ColorSchemes.bone.colors |> reverse |> cgrad
1 Like

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:

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

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

2 Likes