您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python Matplotlib Colorbar科学符号库

Python Matplotlib Colorbar科学符号库

一种可能的解决方案是按以下问题子类化ScalarFormatter并固定其数量级:为多个子图设置具有固定指数和有效数字的科学计数法

这样,你会调用该格式与大小作为参数的顺序orderOOMFormatter(-2, mathText=False)mathText设置为false可以从问题中获取符号,即

在此处输入图片说明

在此处输入图片说明

然后,您可以通过colorbar的format参数将formatter设置为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()

在此处输入图片说明

对于<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)
python 2022/1/1 18:27:19 有246人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶