![](/img/trans.png)
[英]What is the origin of Matplotlib's symlog (a.k.a. symmetrical log) scale?
[英]Symmetrical Log color scale in matplotlib contourf plot
如何为轮廓创建带有符号对数(对称对数)比例的轮廓图。 也就是说,同时显示负值和正值的对数刻度。
一种可能是解决此示例:
http://matplotlib.org/examples/pylab_examples/contourf_log.html
这给出了对数刻度的配方:
from matplotlib import pyplot, ticker
cs = pyplot.contourf(X, Y, z, locator=ticker.LogLocator())
但是,这不允许负值。 有一个ticker.SymmetricalLogLocator()
,这可能是解决方案,但似乎没有太多的文档。
编辑:
为了澄清(因为在对数刻度上请求负值可能听起来很荒谬),我想要的与在matplotlib轴上提供的“ symlog”刻度相同。 下图(取自另一个堆栈交换站 )显示了x轴上的符号。 它是“对数”比例,但是以查看者清楚的方式处理负值。
我想要相同的缩放比例,但是要使用轮廓或轮廓上的色标。
我偶然发现了这个线程,试图做同样的事情,即在正方向和负方向上绘制较大范围的值。 另外,我希望具有与imshow一样的粒度。
事实证明,可以使用“ ticker.MaxNLocator(nbins)”将nbins设置为较高以具有良好的粒度,例如将nbins设置为100。
我还想拥有一种不错的Latex样式代码格式,不久前我在StackOverflow上找到了一种解决方案。
我将在其中的一个类中发布此代码段,以便任何人都可以了解其工作原理。 我使用此解决方案生成多个图,如下图所示。
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.