艺术家对象测试

每个Matplotlib原始艺术家类型的测试单元支持。

轴处理单位转换,艺术家保留指向其父轴的指针。如果要将它们与单位数据一起使用,则必须使用轴实例初始化艺术家,否则他们将不知道如何将单位转换为标量。

此示例需要 basic_units.py

艺术家对象测试示例

  1. import random
  2. import matplotlib.lines as lines
  3. import matplotlib.patches as patches
  4. import matplotlib.text as text
  5. import matplotlib.collections as collections
  6. from basic_units import cm, inch
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. fig, ax = plt.subplots()
  10. ax.xaxis.set_units(cm)
  11. ax.yaxis.set_units(cm)
  12. # Fixing random state for reproducibility
  13. np.random.seed(19680801)
  14. if 0:
  15. # test a line collection
  16. # Not supported at present.
  17. verts = []
  18. for i in range(10):
  19. # a random line segment in inches
  20. verts.append(zip(*inch*10*np.random.rand(2, random.randint(2, 15))))
  21. lc = collections.LineCollection(verts, axes=ax)
  22. ax.add_collection(lc)
  23. # test a plain-ol-line
  24. line = lines.Line2D([0*cm, 1.5*cm], [0*cm, 2.5*cm],
  25. lw=2, color='black', axes=ax)
  26. ax.add_line(line)
  27. if 0:
  28. # test a patch
  29. # Not supported at present.
  30. rect = patches.Rectangle((1*cm, 1*cm), width=5*cm, height=2*cm,
  31. alpha=0.2, axes=ax)
  32. ax.add_patch(rect)
  33. t = text.Text(3*cm, 2.5*cm, 'text label', ha='left', va='bottom', axes=ax)
  34. ax.add_artist(t)
  35. ax.set_xlim(-1*cm, 10*cm)
  36. ax.set_ylim(-1*cm, 10*cm)
  37. # ax.xaxis.set_units(inch)
  38. ax.grid(True)
  39. ax.set_title("Artists with units")
  40. plt.show()

下载这个示例