首页 > 解决方案 > 自定义 matplotlib 偏移中的移位

问题描述

这个问题解决了我目前面临的一个问题。我有一些数据,说[12.301, 12.318, 12.302]在绘制时会导致偏移量显示为+1.23e1.

我不介意偏移量本身,但是这种指数形式而不是仅仅写出来12.3并不是很好。我可以以某种方式强制指数只出现在 10 的 3 次方中吗?1e3而不是1000有意义,1e1而不是10根本没有。

我发现this other question有点相关,但这与只有整数形式更相关,而我不介意小数。

标签: pythonpython-3.xmatplotlib

解决方案


通过在网络上进行一些搜索并调整在此处此处此处找到的答案加上一个print(dir(ScalarFormatter)),我能够调整第一个链接的帖子:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

#from https://stackoverflow.com/a/45332730/2454357
def fexp(f):
    return int(np.floor(np.log10(abs(f)))) if f != 0 else 0

#from https://stackoverflow.com/a/45332730/2454357
def fman(f):
    return f/10**fexp(f)

#adapted from https://stackoverflow.com/a/3679918/2454357
class PowerMultipleOfThreeFormatter(ScalarFormatter):
    """Formats axis ticks using scientific notation with a constant order of 
    magnitude"""
    def __init__(self, useOffset=True, useMathText=False):
        ScalarFormatter.__init__(self, useOffset=useOffset, 
                                 useMathText=useMathText)

    def _set_orderOfMagnitude(self, range):
        """Over-riding this to avoid having orderOfMagnitude reset elsewhere"""
        exponent = fexp(range)
        if -3 < exponent < 3:
            self.orderOfMagnitude = 0
        else:
            new_exp = (exponent//3)*3
            self.orderOfMagnitude = new_exp


    def format_data(self, *args, **kwargs):

        ##make sure that format_data does everyting it shoud:
        super(PowerMultipleOfThreeFormatter, self).format_data(
            *args, **kwargs
        )

        ##compute the offset in the right format
        exponent = fexp(self.offset)
        mantissa = fman(self.offset)
        if -3 < exponent < 3:
            return '{:g}'.format(self.offset)

        new_exp = (exponent//3)*3
        factor = 10**new_exp

        ##from https://stackoverflow.com/a/2440786/2454357
        man_string = '{}'.format(self.offset/factor).rstrip('0').rstrip('.')
        return man_string+'e{}'.format(new_exp)

# Generate some random data...
x = np.linspace(55478, 55486, 100) 
y = np.random.random(100) - 0.5
y = np.cumsum(y)
y *= 1e-8

# Plot the data...
fig,axes = plt.subplots(nrows=2, ncols=2)
for ax, y0 in zip(axes.ravel(), [-1e4, 1.15e-4, 12, -0.1]):
    ax.plot(x, y+y0, 'b-')
    ax.yaxis.set_major_formatter(PowerMultipleOfThreeFormatter())

fig.tight_layout()
plt.show()

一般来说,这个想法是计算一个数字的指数和尾数,并操纵这两者,使指数是 3 的倍数(使用(exponent//3)*3exponent%3)。对于乘数,这里已经演示了这些计算应该在何处以及如何添加(即在 中_set_orderOfMagnitude)。偏移值存储在函数中ScalarFormatter.offset,字符串表示在函数中计算format_data()。重载该函数,我们可以更改偏移量的显示方式。该代码还包含一个如何使用新格式化程序的示例(如何生成数据的方式再次无耻地从这里复制)。代码的结果如下所示:

上述代码的结果


推荐阅读