使用工程符号标记刻度线

使用工程格式化程序。

工程格式化示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.ticker import EngFormatter
  4. # Fixing random state for reproducibility
  5. prng = np.random.RandomState(19680801)
  6. # Create artificial data to plot.
  7. # The x data span over several decades to demonstrate several SI prefixes.
  8. xs = np.logspace(1, 9, 100)
  9. ys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2
  10. # Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.
  11. fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(7, 9.6))
  12. for ax in (ax0, ax1):
  13. ax.set_xscale('log')
  14. # Demo of the default settings, with a user-defined unit label.
  15. ax0.set_title('Full unit ticklabels, w/ default precision & space separator')
  16. formatter0 = EngFormatter(unit='Hz')
  17. ax0.xaxis.set_major_formatter(formatter0)
  18. ax0.plot(xs, ys)
  19. ax0.set_xlabel('Frequency')
  20. # Demo of the options `places` (number of digit after decimal point) and
  21. # `sep` (separator between the number and the prefix/unit).
  22. ax1.set_title('SI-prefix only ticklabels, 1-digit precision & '
  23. 'thin space separator')
  24. formatter1 = EngFormatter(places=1, sep="\N{THIN SPACE}") # U+2009
  25. ax1.xaxis.set_major_formatter(formatter1)
  26. ax1.plot(xs, ys)
  27. ax1.set_xlabel('Frequency [Hz]')
  28. plt.tight_layout()
  29. plt.show()

下载这个示例