# Divergent colormap with white near center

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.)

A sample Julia code to illustrate the problem:

``````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)
``````

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.

1 Like

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!

``````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)
``````