Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace colors of a colormap respresenting the two smallest values by white in matplolib?

I am plotting an animated contourf map in matplotlib with a colorbar that changes at each frame. I want to keep the colorbar centered at zero (I am using a diverging colormap) and to do so I use an odd number of levels. The problem is, when I do this, even though the central color of the colormap (cm.seismic) is white, this color does not appear in the colormap. I want to be able to replace the color of the smallest values (the light red and the light blue) by white, so that instead of having one central level whose color is white (zero), I have two (two smallest values).

What I have / What I want

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of providing a colormap, you can provide a list of colors. That list can be calculated from the given colormap, and the middle colors can be set explicitly to white.

Here is an example:

import matplotlib.pyplot as plt
import numpy as np

x = y = np.linspace(-3.0, 3.01, 100)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X ** 2 - Y ** 2)
Z2 = np.exp(-(X - 1) ** 2 - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2

num_levels = 9
colors = plt.cm.seismic(np.linspace(0, 1, num_levels + 1))
colors[num_levels // 2] = [1, 1, 1, 1] # set to white
colors[num_levels // 2 + 1] = [1, 1, 1, 1]

fig, ax1 = plt.subplots(figsize=(10, 5))
CS = ax1.contourf(X, Y, Z, num_levels, colors=colors, origin='lower')
cbar = fig.colorbar(CS, ax=ax1)
plt.show()

plt.contourf with white at center

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading