Custom color gradient transition points for heatmap in Plots using pyplot backend


I am trying to plot a heatmap using a custom color gradient. More precisely, I want to space out the colors in a particular way.

However, I am unable to set the color gradient appropriately using the pyplot package (it works as expected on gr). Here is the minimally working example:

using Plots      

pyplot() # replace to gr() to use GR backend

# defining color gradient
col_grad = cgrad([:blue, :white, :orange], [0.45, 0.55])

# plotting heatmap
heatmap(rand(10, 10), color = col_grad)

which gives:

However, the same code using gr instead of pyplot yields:

I need the color gradient to have a very narrow range for the white color. It seems as if pyplot does not take into account how the “color steps” are specified in the cgrad function.

Is there a way to force the pyplot backend to respect this spacing? And is this behaviour normal?

Thanks a lot!

One possibility that I found is to construct the color gradient without ColorSchemes, I can do somehthing along the lines of this:

o_to_w = [weighted_color_mean(grad_func(x), colorant"white", colorant"darkorange2") for x in LinRange(0, 1, 100)]
w_to_b = reverse([weighted_color_mean(grad_func(x), colorant"white", colorant"blue3") for x in LinRange(0, 1, 100)])
col_grad = vcat(o_to_w, w_to_b)

If this is possible, I’m a little puzzled as to why the ColorSchemes does not work…

I would simply do:

col_grad = cgrad([:blue, :blue, :white, :orange, :orange], [0, 0.45, 0.5, 0.55, 1])

Or, perhaps prettier:

col_grad = cgrad([:darkblue, :blue, :white, :orange, :darkorange], [0, 0.45, 0.5, 0.55, 1])

Hmm, it does not appear to work for me…

Putting cgrad([:blue, :blue, :white, :orange, :orange], [0, 0.1, 0.5, 0.9, 1]) gives me this:

and changing the colors’ separation, like cgrad([:blue, :blue, :white, :orange, :orange], [0, 0.45, 0.5, 0.55, 1]) gives me the same result:

This is much closer to the GR results.

Right, it definitely makes it closer to what I need, but the reason is because we’ve added more colors to the list and this way shifted the transition points.

Unfortunately, specifying these transition points by simply adding more color points is not a viable solution for my purposes haha

I guess the main issue is that pyplot is not reading the transition points?