简体   繁体   English

Matplotlib Contourf图中的对称对数色标

[英]Symmetrical Log color scale in matplotlib contourf plot

How do I create a contour plot with a symlog (symmetrical log) scale for the contours. 如何为轮廓创建带有符号对数(对称对数)比例的轮廓图。 ie a log scale that shows both negative and positive values. 也就是说,同时显示负值和正值的对数刻度。

One possibility would be to work off of this example: 一种可能是解决此示例:

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

Which gives this recipe for a log scale: 这给出了对数刻度的配方:

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

However, this doesn't allow for negative values. 但是,这不允许负值。 There is a ticker.SymmetricalLogLocator() , which may be the solution, but it doesn't seem to have much documentation. 有一个ticker.SymmetricalLogLocator() ,这可能是解决方案,但似乎没有太多的文档。

EDIT: 编辑:

To clarify (since requesting negative values on a log scale may sound nonsensical), what I want is the same as the "symlog" scale provided on matplotlib axes. 为了澄清(因为在对数刻度上请求负值可能听起来很荒谬),我想要的与在matplotlib轴上提供的“ symlog”刻度相同。 The plot below, (taken from another stack exchange post ), shows symlog on the x-axis. 下图(取自另一个堆栈交换站 )显示了x轴上的符号。 It is a "log" scale, but handles negative values in a way that is clear to the viewer. 它是“对数”比例,但是以查看者清楚的方式处理负值。

符号示例

I want the same sort of scaling, but for the colorscale on contour or contourf. 我想要相同的缩放比例,但是要使用轮廓或轮廓上的色标。

I stumbled across this thread trying to do the same thing, ie plotting a large range of values in both the positive and negative direction. 我偶然发现了这个线程,试图做同样的事情,即在正方向和负方向上绘制较大范围的值。 In addition I wanted to have a granularity as fine as in imshow. 另外,我希望具有与imshow一样的粒度。

It turns out you can have that using "ticker.MaxNLocator(nbins)" where nbins can be set high to have a fine granularity, eg set nbins to 100. 事实证明,可以使用“ ticker.MaxNLocator(nbins)”将nbins设置为较高以具有良好的粒度,例如将nbins设置为100。

I also wanted to have a nice Latex style ticker formatting, for which I found a solution on StackOverflow a while ago. 我还想拥有一种不错的Latex样式代码格式,不久前我在StackOverflow上找到了一种解决方案。

I will just post this code snippet here from one of the classes it is part of so that anyone who might want can get the basic idea about how it's working. 我将在其中的一个类中发布此代码段,以便任何人都可以了解其工作原理。 I use this solution to generate multiple plots as shown in the image below. 我使用此解决方案生成多个图,如下图所示。

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

在此处输入图片说明

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

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