![](/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.