英寸和厘米

该示例说明了使用绘图函数的xunits和yunits参数将默认x和y单位(ax1)覆盖为英寸和厘米的功能。 请注意,应用转换以获取正确单位的数字。

此示例需要basic_units.py

英寸和厘米示例

  1. from basic_units import cm, inch
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. cms = cm * np.arange(0, 10, 2)
  5. fig, axs = plt.subplots(2, 2)
  6. axs[0, 0].plot(cms, cms)
  7. axs[0, 1].plot(cms, cms, xunits=cm, yunits=inch)
  8. axs[1, 0].plot(cms, cms, xunits=inch, yunits=cm)
  9. axs[1, 0].set_xlim(3, 6) # scalars are interpreted in current units
  10. axs[1, 1].plot(cms, cms, xunits=inch, yunits=inch)
  11. axs[1, 1].set_xlim(3*cm, 6*cm) # cm are converted to inches
  12. plt.show()

下载这个示例