自定义刻度#

The 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 生成的图库