Skip to content Skip to sidebar Skip to footer

Symmetrical Log Color Scale In Matplotlib Contourf Plot

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 w

Solution 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 plotsdeffmt(x, pos):
  a, b = '{:.2e}'.format(x).split('e')
  b = int(b)
  returnr'${} \times 10^{{{}}}$'.format(a, b)

# A confourf function I use inside one of my classes# mainly interesting are the "plot" and "cbar" linesdefMake2DSubPlot(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])

enter image description here

Post a Comment for "Symmetrical Log Color Scale In Matplotlib Contourf Plot"