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

**URL:** <https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724>\
**Category:** Visualization\
**Tags:** question, plotting, pyplot, plots\
**Created:** [July 7, 2024, 3:08pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724 "2024-07-07T15:08:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [July 7, 2024, 3:08pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/1 "2024-07-07T15:08:17Z")

</div>

Hello,

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:

```julia
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:

 ![Capture d’écran, le 2024-07-07 à 17.01.33](https://global.discourse-cdn.com/julialang/original/3X/2/7/27e5967f391321701e85a4e83a9ba1bf7c00d8b6.png)

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

 ![Capture d’écran, le 2024-07-07 à 17.03.11](https://global.discourse-cdn.com/julialang/original/3X/9/a/9abaae92f3ff960dce0f7f7e9a275acbc3c1c0fe.png)

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!

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [July 7, 2024, 3:39pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/2 "2024-07-07T15:39:45Z")

</div>

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

```julia
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…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 7, 2024, 4:38pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/3 "2024-07-07T16:38:56Z")

</div>

I would simply do:

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

```

Or, perhaps prettier:

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

```

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [July 7, 2024, 5:17pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/4 "2024-07-07T17:17:35Z")

</div>

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:

 ![Capture d’écran, le 2024-07-07 à 18.50.05](https://global.discourse-cdn.com/julialang/original/3X/5/4/54afbed4008644ac704eb5ed3ac4e3ba8ac07baf.png)

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:

 ![Capture d’écran, le 2024-07-07 à 19.17.04](https://global.discourse-cdn.com/julialang/original/3X/a/9/a99854e5e339087275e877e9e69673641593b98a.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 7, 2024, 5:37pm UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/5 "2024-07-07T17:37:40Z")

</div>

This is much closer to the GR results.

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [July 8, 2024, 8:24am UTC](https://discourse.julialang.org/t/custom-color-gradient-transition-points-for-heatmap-in-plots-using-pyplot-backend/116724/6 "2024-07-08T08:24:55Z")

</div>

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?
