Symmetrical Log color scale in matplotlib contourf plot

前端 未结 1 1821
逝去的感伤
逝去的感伤 2021-01-22 12:49

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 wo

1条回答
  •  天涯浪人
    2021-01-22 13:26

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

    0 讨论(0)
提交回复
热议问题