我偶然发现了这个线程,试图做同样的事情,即在正方向和负方向上绘制大范围的值。此外,我希望拥有像 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])