简体   繁体   English

如何获取matplotlib制作颜色以0为中心的颜色栏?

[英]How can I get matplotlib to make a colorbar with a color centered on 0?

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: 我正在绘制一些数据的Wigner分布,在分布的极端部分,该值趋向于接近0。它也趋于振荡很多,使我的测试图如下所示: 威格纳分布图

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. 蓝色“海”中的波的振幅大约为1E-18,但是由于它们在0左右振荡,因此它们会穿过颜色栏中的一条线。

How can I make a contourf() plot that has a color centered on zero instead of having zero as a boundary value? 我怎样才能让已经颜色中心 ,而不必零边界值在零contourf()情节?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM