2

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: Plot of a Wigner distribution

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?

Dan
  • 12,157
  • 12
  • 50
  • 84
  • didn't quite understand your question. But you can do the following trick: `data[data>2]=2.0` or use `clim` to specify the limits of the color axis – Sleepyhead Apr 29 '14 at 00:12
  • The trick is going to be to modify your colormap. If you aren't already using one, you'll need to make one and use it. Let me know if you need more detail, and I'll write an answer. – Justin Fletcher Apr 29 '14 at 01:49

1 Answers1

1

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()
The Dude
  • 3,795
  • 5
  • 29
  • 47