Matplotlib Contourf图中的对称对数色标 [英] Symmetrical Log color scale in matplotlib contourf plot

查看:240
本文介绍了Matplotlib Contourf图中的对称对数色标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为轮廓创建带有符号对数(对称对数)比例的轮廓图。即同时显示负值和正值的对数刻度。



一种可能是解决此示例:




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.

解决方案

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

这篇关于Matplotlib Contourf图中的对称对数色标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆