Python matplotlib colorbar科学符号库

Python matplotlib colorbar科学符号库,python,matplotlib,colorbar,contourf,Python,Matplotlib,Colorbar,Contourf,我试图在我的matpllotlib轮廓图上定制一个色条。虽然我能够使用科学记数法,但我正在尝试改变记数法的基础-基本上,这样我的刻度将在(-100100)范围内,而不是(-10,10) 例如,这将生成一个简单的绘图 import numpy as np import matplotlib.pyplot as plt z = (np.random.random((10,10)) - 0.5) * 0.2 fig, ax = plt.subplots() plot = ax.contourf(z

我试图在我的matpllotlib轮廓图上定制一个色条。虽然我能够使用科学记数法,但我正在尝试改变记数法的基础-基本上,这样我的刻度将在(-100100)范围内,而不是(-10,10)

例如,这将生成一个简单的绘图

import numpy as np
import matplotlib.pyplot as plt

z = (np.random.random((10,10)) - 0.5) * 0.2

fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot)

cbar.formatter.set_powerlimits((0, 0))
cbar.update_ticks()

plt.show()
像这样:

但是,我希望颜色条上方的标签为1e-2,数字范围为-10到10


我该怎么做呢?

一个可能的解决方案是将
ScalarFormatter子类化,并固定数量级,如本问题所示:

然后调用此格式化程序,其数量级作为参数
order
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_order_of_magnitude(self):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin=None, vmax=None):
        self.format = self.fformat
        if self._useMathText:
             self.format = r'$\mathdefault{%s}$' % 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()

对于matplotlib版本<3.1,类需要如下所示:

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)

与@ImportanceOfBeingErnes所描述的类似,您可以使用
FuncFormatter
(),只需向其传递一个函数即可确定勾号标签。这将删除颜色栏的
1e-2
标题的自动生成,但我想您可以手动将其添加回(我在执行此操作时遇到了问题,但能够将其添加到侧面)。使用
FuncFormatter
,您只需生成字符串记号值,其优点是不必接受python认为数字应该显示的方式

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tk

z = (np.random.random((10,10)) - 0.5) * 0.2

levels = list(np.linspace(-.1,.1,9))

fig, ax = plt.subplots()
plot = ax.contourf(z, levels=levels)

def my_func(x, pos):
    label = levels[pos]
    return str(label*100)

fmt1 = tk.FuncFormatter(my_func)

cbar = fig.colorbar(plot, format=fmt1)
cbar.set_label("1e-2")

plt.show()
这将生成一个如下所示的绘图


人们还能复制这种情况吗?