自定义刻度定位器#

matplotlib.ticker 模块定义了许多预设的刻度定位器,但其主要设计目的是为了可扩展性,即支持用户自定义刻度设置。

在此示例中,使用用户自定义函数来格式化Y轴上的刻度,以百万美元为单位显示。

import matplotlib.pyplot as plt


def millions(x, pos):
    """The two arguments are the value and tick position."""
    return f'${x*1e-6:1.1f}M'


fig, ax = plt.subplots()
# set_major_formatter internally creates a FuncFormatter from the callable.
ax.yaxis.set_major_formatter(millions)
money = [1.5e5, 2.5e6, 5.5e6, 2.0e7]
ax.bar(['Bill', 'Fred', 'Mary', 'Sue'], money)
plt.show()
custom ticker1

参考

本示例展示了以下函数、方法、类和模块的使用

由 Sphinx-Gallery 生成的画廊