# Divergent colormap with white near center

**URL:** <https://discourse.julialang.org/t/divergent-colormap-with-white-near-center/118215>\
**Category:** Visualization\
**Tags:** question, makie, colormaps\
**Created:** [August 15, 2024, 9:07am UTC](https://discourse.julialang.org/t/divergent-colormap-with-white-near-center/118215 "2024-08-15T09:07:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [August 15, 2024, 9:07am UTC](https://discourse.julialang.org/t/divergent-colormap-with-white-near-center/118215/1 "2024-08-15T09:07:04Z")

</div>

I’m plotting a 2D field with `contourf`. The field has positive and negative values and so I use one divergent colormap or another and with a symmetric contouring like `levels=-10:2:10`.

The problem I face now is that I need to delete the zero-value contour lines. Setting the levels like `levels=-11:2:11` is one way, but with this strategy, it’s harder to get “clean” numbers for contour levels. I want ticks on the colorbar where the contour levels are: something like `Colorbar( . . . ; ticks=levels)`.

What I want is a white or gray region around zero, as in the attached plot. The colorbar is sort of ugly but as a scientific presentation, it’s sometimes better to prevent the reader from focusing on the zero contour lines. I mean, we sometimes want to ignore small absolute values.

This problem becomes significant when you plot a set of plots for a movie. You want to keep the contour levels constant across the frames and sometimes a large fraction of the frame is occupied by near-zero values, for which you want to show just solid white or gray, without zero-level contour lines.

So, are there divergent colormaps with a white or gray “region” around the center?

If not, what’s the best way to edit an existing colormap? (It would be nice if the modification can be done within the Julia program to plot the contour maps.)

 ![graymiddle](https://global.discourse-cdn.com/julialang/original/3X/8/8/88e1164d779bf49871f20add688acbd13733c1b8.png)

A sample Julia code to illustrate the problem:

```julia
using CairoMakie

nx = 30
ny = 20
xax = (-nx/2):(nx/2)
yax = (-ny/2):(ny/2)
myfield = rand(nx+1,ny+1) .- 0.5

fig = Figure()
ax = Axis(fig[1,1])
c = contourf!(xax,yax,myfield;levels=-5:1:5,colormap=:RdBu)
Colorbar(fig[1,2], c)
save("tmp3.png", 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:** [August 15, 2024, 1:38pm UTC](https://discourse.julialang.org/t/divergent-colormap-with-white-near-center/118215/2 "2024-08-15T13:38:22Z")

</div>

You don’t have to use a range, you could do a vector where zero is removed. For example `levels = filter(!=(0), -10:10)`. Then you don’t need a larger white region in the colormap to hide the data.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [August 16, 2024, 5:13am UTC](https://discourse.julialang.org/t/divergent-colormap-with-white-near-center/118215/3 "2024-08-16T05:13:52Z")

</div>

> [@jules](#):
>
> You don’t have to use a range, you could do a vector where zero is removed.

Ah! You are smart. Yes.

I put my sample program below modified to incorporate your idea. You don’t have to look at it. I just report that it gives me the desired result.

If I understand the mechanism correctly . . . When the contouring levels are . . . , -2, -1, 1, 2, . . . , `Colorbar` first calculates the midpoint between -1 and 1 (which is 0) and use the corresponding color (which is white) for the [-1, 1] interval. That makes sense!

```julie
using CairoMakie

nx = 30
ny = 20
xax = (-nx/2):(nx/2)
yax = (-ny/2):(ny/2)
myfield = 8*rand(nx+1,ny+1) .- 4.0

levels = -5:1:5
levels_filtered = filter(!=(0), levels) # remove 0

fig = Figure()
ax = Axis(fig[1,1])
c = contourf!(xax,yax,myfield;levels=levels_filtered,colormap=:RdBu)
Colorbar(fig[1,2], c; ticks=levels)
save("tmp3.png", fig)

```
