I'm plotting a Wigner distribution of some data, and at extreme parts of the distribution the value tends to be near 0. It also tends to oscillate a lot, making my test plot look like this:
The waves in the blue "sea" have an amplitude around 1E-18, but because they oscillate around 0 they cross a line in the colorbar.
How can I make a contourf() plot that has a color centered on zero instead of having zero as a boundary value?
I think you want something like this:
class set_cbar_zero(Normalize):
"""
set_cbar_zero(midpoint = float) default: midpoint = 0.
Normalizes and sets the center of any colormap to the desired valua which
is set using midpoint.
"""
def __init__(self, vmin = None, vmax = None, midpoint = 0., clip = False):
self.midpoint = midpoint
Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip = None):
x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
return numpy.ma.masked_array(numpy.interp(value, x, y))
You can then normalize the colorbar which should solve your problem, if I understood it correctly
import numpy
import matplotlib.pylab as plt
data = numpy.random.random((30, 95))
plt.figure()
plt.imshow(data, cmap = "hot", norm = set_cbar_zero())
plt.show()