Symmetrical Log color scale in matplotlib contourf

2019-08-06 21:54发布

问题:

How do I create a contour plot with a symlog (symmetrical log) scale for the contours. i.e. 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

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.

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. The plot below, (taken from another stack exchange post), shows symlog on the x-axis. 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.

回答1:

I stumbled across this thread trying to do the same thing, i.e 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.

It turns out you can have that using "ticker.MaxNLocator(nbins)" where nbins can be set high to have a fine granularity, e.g. set nbins to 100.

I also wanted to have a nice Latex style ticker formatting, for which I found a solution on StackOverflow a while ago.

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