【问题标题】:Symmetrical Log color scale in matplotlib contourf plotmatplotlib contourf 图中的对称对数色标
【发布时间】:2014-12-18 13:40:48
【问题描述】:

如何使用等高线的 symlog(对称对数)比例创建等高线图。即显示负值和正值的对数刻度。

一种可能性是解决这个例子:

http://matplotlib.org/examples/pylab_examples/contourf_log.html

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

from matplotlib import pyplot, ticker
cs = pyplot.contourf(X, Y, z, locator=ticker.LogLocator())

但是,这不允许负值。有一个ticker.SymmetricalLogLocator(),这可能是解决方案,但它似乎没有太多文档。

编辑:

为了澄清(因为在对数刻度上请求负值可能听起来很荒谬),我想要的与 matplotlib 轴上提供的“symlog”刻度相同。下图(取自another stack exchange post)在 x 轴上显示符号日志。它是一个“对数”比例,但以一种对查看者来说很清楚的方式处理负值。

我想要相同的缩放比例,但对于轮廓或轮廓上的色阶。

【问题讨论】:

  • “负值”是什么意思?数字
  • 当然,所以对于负值,您需要 -1*log(abs(x)),其中 x 是对数。这在 matplotlib 中被称为 1D 轴的“symlog”(对称日志)。见:stackoverflow.com/questions/3305865/…
  • 啊哈,感谢您的澄清。您提到的SymmetricalLogLocator 的源代码可以在ticker.py 中找到,并且从那里的代码 cmets 我相信它应该可以满足您的需求。看看它,我觉得你可能已经回答了你自己的问题。

标签: python python-2.7 matplotlib contour contourf


【解决方案1】:

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

事实证明,您可以使用“ticker.MaxNLocator(nbins)”来实现,其中 nbins 可以设置得较高以具有精细的粒度,例如将 nbins 设置为 100。

我还想要一个漂亮的 Latex 风格的股票行情格式,我不久前在 StackOverflow 上找到了一个解决方案。

我将把这段代码 sn-p 从它所属的一个类中发布到这里,以便任何可能想要的人都可以了解它是如何工作的。我使用此解决方案生成多个图,如下图所示。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2011-08-10
    • 1970-01-01
    • 2023-04-04
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多