Transoffset

这说明了使用transforms.offset_copy进行变换,该变换将绘图元素(如文本字符串)定位在屏幕坐标(点或英寸)中相对于任何坐标中给出的位置的指定偏移处。

每个Artist - 从中派生Text和Line等类的mpl类 - 都有一个可以在创建Artist时设置的转换,例如通过相应的pyplot命令。 默认情况下,这通常是Axes.transData转换,从数据单元到屏幕点。 我们可以使用offset_copy函数来修改此转换的副本,其中修改包含偏移量。

Transoffset示例

  1. import matplotlib.pyplot as plt
  2. import matplotlib.transforms as mtransforms
  3. import numpy as np
  4. xs = np.arange(7)
  5. ys = xs**2
  6. fig = plt.figure(figsize=(5, 10))
  7. ax = plt.subplot(2, 1, 1)
  8. # If we want the same offset for each text instance,
  9. # we only need to make one transform. To get the
  10. # transform argument to offset_copy, we need to make the axes
  11. # first; the subplot command above is one way to do this.
  12. trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
  13. x=0.05, y=0.10, units='inches')
  14. for x, y in zip(xs, ys):
  15. plt.plot((x,), (y,), 'ro')
  16. plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset)
  17. # offset_copy works for polar plots also.
  18. ax = plt.subplot(2, 1, 2, projection='polar')
  19. trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
  20. y=6, units='dots')
  21. for x, y in zip(xs, ys):
  22. plt.polar((x,), (y,), 'ro')
  23. plt.text(x, y, '%d, %d' % (int(x), int(y)),
  24. transform=trans_offset,
  25. horizontalalignment='center',
  26. verticalalignment='bottom')
  27. plt.show()

下载这个示例