栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python Matplotlib Colorbar科学符号库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Matplotlib Colorbar科学符号库

一种可能的解决方案是按以下问题子类化

ScalarFormatter
并固定其数量级:为多个子图设置具有固定指数和有效数字的科学计数法

这样,你会调用该格式与大小作为参数的顺序

order
OOMFormatter(-2,mathText=False)
mathText
设置为false可以从问题中获取符号,即

,将其设置为True时,将给出。

然后,您可以通过colorbar的

format
参数将formatter设置为colorbar 。

import numpy as np; np.random.seed(0)import matplotlib.pyplot as pltimport matplotlib.tickerclass 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.formatz = (np.random.random((10,10)) - 0.5) * 0.2fig, 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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号