Python matplotlib轮廓图中的对称对数颜色比例

Python matplotlib轮廓图中的对称对数颜色比例,python,python-2.7,matplotlib,contour,contourf,Python,Python 2.7,Matplotlib,Contour,Contourf,如何为等高线创建具有symlog(对称对数)比例的等高线图。i、 e.显示负值和正值的对数刻度 一种可能是利用这个例子: 它给出了对数刻度的配方: from matplotlib import pyplot, ticker cs = pyplot.contourf(X, Y, z, locator=ticker.LogLocator()) 但是,这不允许出现负值。有一个ticker.SymmetricalLogLocator(),它可能是解决方案,但似乎没有太多文档 编辑: 为了澄清(因为在

如何为等高线创建具有symlog(对称对数)比例的等高线图。i、 e.显示负值和正值的对数刻度

一种可能是利用这个例子:

它给出了对数刻度的配方:

from matplotlib import pyplot, ticker
cs = pyplot.contourf(X, Y, z, locator=ticker.LogLocator())
但是,这不允许出现负值。有一个
ticker.SymmetricalLogLocator()
,它可能是解决方案,但似乎没有太多文档

编辑:

为了澄清(因为在对数刻度上请求负值听起来可能毫无意义),我想要的与matplotlib轴上提供的“symlog”刻度相同。下图(取自)显示了x轴上的symlog。它是一个“对数”刻度,但处理负值的方式对查看者来说是清晰的


我想要同样的缩放,但是对于contour或contourf上的colorscale。

我偶然发现这个线程试图做同样的事情,即在正方向和负方向绘制大范围的值。此外,我希望拥有与imshow一样精细的粒度

事实证明,您可以使用“ticker.MaxNLocator(nbins)”将nbins设置为高粒度,例如将nbins设置为100

我还想有一个很好的Latex风格的股票格式,我在StackOverflow上找到了一个解决方案

我将在这里发布它所属的一个类的代码片段,以便任何想要了解它如何工作的人都能了解它的基本原理。我使用此解决方案生成多个绘图,如下图所示

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# function for nice Latex style tick formatting
# copied from
# http://stackoverflow.com/questions/25983218/
# scientific-notation-colorbar-in-matplotlib
# output formating for colorbar in 2D plots
def fmt(x, pos):
  a, b = '{:.2e}'.format(x).split('e')
  b = int(b)
  return r'${} \times 10^{{{}}}$'.format(a, b)

# A confourf function I use inside one of my classes
# mainly interesting are the "plot" and "cbar" lines
def Make2DSubPlot(self, posIdent, timeIdx,typeIdx):
  plt.subplot(posIdent)
  y = self.radPos
  x = self.axPos
  z = self.fieldList[timeIdx][typeIdx]
  plot = plt.contourf(x, y, z, locator=ticker.MaxNLocator(100), \
          aspect='auto',origin='lower')
  cbar = plt.colorbar(plot, orientation='vertical', \
          format=ticker.FuncFormatter(fmt))
  cbar.ax.set_ylabel(self.labelList[typeIdx])
  plt.xlabel(self.labelList[self.iax])
  plt.ylabel(self.labelList[self.iax])

你说的“负值”是什么意思?数字的日志是确定的,所以对于负值,需要-1*log(abs(x)),其中x是日志。在matplotlib中,这被称为1D轴的“symlog”(对称日志)。看:啊哈,谢谢你的澄清。您提到的
SymmetricalLogLocator
的源代码可以在
ticker.py
中找到,从那里的代码注释中,我相信它应该满足您的要求。仔细想想,我觉得你可能已经回答了你自己的问题。