一个可能的解决方案可以是ScalarFormatter的子类并修复此问题中的数量级:Set scientific notation with fixed exponent and significant digits for multiple subplots
然后,您将调用此格式化程序,其数量级为参数顺序,OOMFormatter(-2,mathText = False). mathText设置为false以从问题中获取符号,即
将其设置为True时,将给出.
然后,您可以通过colorbar的format参数将格式化程序设置为colorbar.
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.ticker
class OOMFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
self.oom = order
self.fformat = fformat
matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
def _set_orderOfMagnitude(self, nothing):
self.orderOfMagnitude = self.oom
def _set_format(self, vmin, vmax):
self.format = self.fformat
if self._useMathText:
self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)
z = (np.random.random((10,10)) - 0.5) * 0.2
fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot, format=OOMFormatter(-2, mathText=False))
plt.show()