自定义Ticker1

新的自动收报机代码旨在明确支持用户自定义滴答。matplotlib.ticker 的文档详细介绍了此过程。 该代码定义了许多预设代码,但主要设计为用户可扩展。

在此示例中,用户定义的函数用于格式化y轴上数百万美元的刻度。

自定义Ticker1示例

  1. from matplotlib.ticker import FuncFormatter
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. x = np.arange(4)
  5. money = [1.5e5, 2.5e6, 5.5e6, 2.0e7]
  6. def millions(x, pos):
  7. 'The two args are the value and tick position'
  8. return '$%1.1fM' % (x * 1e-6)
  9. formatter = FuncFormatter(millions)
  10. fig, ax = plt.subplots()
  11. ax.yaxis.set_major_formatter(formatter)
  12. plt.bar(x, money)
  13. plt.xticks(x, ('Bill', 'Fred', 'Mary', 'Sue'))
  14. plt.show()

下载这个示例