繁体   English   中英

Python matplotlib colorbar 科学记数法基础

[英]Python matplotlib colorbar scientific notation base

我正在尝试在我的 matplotlib 轮廓图上自定义颜色条。 虽然我能够使用科学记数法,但我正在尝试更改记数法的基础 - 基本上是这样我的刻度将在 (-100,100) 而不是 (-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进行子类ScalarFormatter并像在这个问题中那样固定数量级: Set ScalarFormatter notation with fixed exponent and ScalarFormatter for multiple subplots

然后,您将使用数量级作为参数order调用此格式化程序, OOMFormatter(-2, mathText=False) mathText设置为 false 以从问题中获取符号,即在此处输入图片说明 将其设置为 True 时,会给在此处输入图片说明 .

然后,您可以通过颜色栏的format参数将格式化程序设置为颜色栏。

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()

在此处输入图片说明

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

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 ( docs ),您只需向其传递一个函数来确定刻度标签。 这会为您的颜色条删除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()

这将生成一个看起来像这样的图。

等高线图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM