Fix Python – Defining the midpoint of a colormap in matplotlib

Question

Asked By – tillsten

I want to set the middle point of a colormap, i.e., my data goes from -5 to 10 and I want zero to be the middle point. I think the way to do it is by subclassing normalize and using the norm, but I didn’t find any example and it is not clear to me, what exactly have I to implement?

Now we will see solution for issue: Defining the midpoint of a colormap in matplotlib


Answer

Note that in matplotlib version 3.2+ the TwoSlopeNorm class was added. I think it covers your use case.
It can be used like this:

from matplotlib import colors
divnorm=colors.TwoSlopeNorm(vmin=-5., vcenter=0., vmax=10)
pcolormesh(your_data, cmap="coolwarm", norm=divnorm)

In matplotlib 3.1 the class was called DivergingNorm.

This question is answered By – macKaiver

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0