【问题标题】:Set scientific notation with fixed exponent and significant digits for multiple subplots为多个子图设置具有固定指数和有效数字的科学记数法
【发布时间】:2017-03-07 18:58:53
【问题描述】:

我正在尝试将轴固定为两组不同数据的科学记数法,其中一组是 [1-9]x1e-3,另一组是 [1-9]x1e-4。我想将两个轴都设置为 10^-4 并在小数点后保留一位(例如 %.1e)。这是我尝试过的一个简单版本:我希望轴上的数字至少为 1,并且我希望两个幂相同。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1,9,9)
y1 = x*10**(-4)
y2 = x*10**(-3)

fig, ax = plt.subplots(2,1,sharex=True)

ax[0].plot(x,y1)
ax[0].ticklabel_format(axis='y', style='sci', scilimits=(-4,-4))
ax[0].yaxis.major.formatter._useMathText = True
ax[1].plot(x,y2)
ax[1].ticklabel_format(axis='y', style='sci', scilimits=(-4,-4))
ax[1].yaxis.major.formatter._useMathText = True

plt.show()

【问题讨论】:

    标签: python-2.7 matplotlib label axes


    【解决方案1】:

    您可以将matplotlib.ticker.ScalarFormatter 子类化并将orderOfMagnitude 属性固定为您喜欢的数字(在本例中为-4)。
    以同样的方式,您可以修复要使用的格式。

    import numpy as np
    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
    
    
    x = np.linspace(1,9,9)
    y1 = x*10**(-4)
    y2 = x*10**(-3)
    
    fig, ax = plt.subplots(2,1,sharex=True)
    
    ax[0].plot(x,y1)
    ax[1].plot(x,y2)
    
    for axe in ax:
        axe.yaxis.set_major_formatter(OOMFormatter(-4, "%1.1f"))
        axe.ticklabel_format(axis='y', style='sci', scilimits=(-4,-4))
    
    plt.show()
    

    虽然这乍一看似乎很复杂,但它真正做的唯一事情是覆盖私有方法 _set_orderOfMagnitude 和 _set_format,从而防止它们在后台执行一些我们不想要的复杂操作。因为最后,我们需要的只是,不管内部发生什么,self.orderOfMagnitude 始终是 -4,self.format 始终是 "%1.1f"。

    注意:在 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=None):
                self.orderOfMagnitude = self.oom
            def _set_format(self, vmin=None, vmax=None):
                self.format = self.fformat
                if self._useMathText:
                    self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)
    

    【讨论】:

    • 这行得通!我只是想知道您是否可以解释类中的每个定义在做什么,是否需要对其进行扩展以修复 sig 无花果的数量,例如,如果我想要小数点后的一个(例如 1.835 e-3 -> 18.3 e-4) @ImportanceOfBeingErnest
    • 谢谢!接受答案
    • 我知道晚了,但是如果 matplotlib 版本
    • 很公平。错误如下: self.format = '$%s$' % ticker._mathdefault(self.format) AttributeError: 'module' object has no attribute '_mathdefault'
    • 在 matplotlib 3.1.1 中看起来帮助器签名略有变化,一个将名称更改为蛇形大小写,并且都不接受 args,所以 def _set_order_of_magnitude(self): ... 和 def _set_format(self): ...
    猜你喜欢
    • 2017-02-10
    • 2012-01-05
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2022-06-12
    • 2011-11-21
    相关资源
    最近更新 更多