相对线的文本旋转

matplotlib中的文本对象通常相对于屏幕坐标系旋转(即,无论轴如何更改,沿水平和垂直之间的直线旋转45度打印文本)。但是,有时需要围绕打印上的某个内容旋转文本。在这种情况下,正确的角度将不是该对象在打印坐标系中的角度,而是该对象在屏幕坐标系中显示的角度。此角度是通过将角度从打印转换为屏幕坐标系来找到的,如下面的示例所示。

相对线的文本旋转示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Plot diagonal line (45 degrees)
  4. h = plt.plot(np.arange(0, 10), np.arange(0, 10))
  5. # set limits so that it no longer looks on screen to be 45 degrees
  6. plt.xlim([-10, 20])
  7. # Locations to plot text
  8. l1 = np.array((1, 1))
  9. l2 = np.array((5, 5))
  10. # Rotate angle
  11. angle = 45
  12. trans_angle = plt.gca().transData.transform_angles(np.array((45,)),
  13. l2.reshape((1, 2)))[0]
  14. # Plot text
  15. th1 = plt.text(l1[0], l1[1], 'text not rotated correctly', fontsize=16,
  16. rotation=angle, rotation_mode='anchor')
  17. th2 = plt.text(l2[0], l2[1], 'text rotated correctly', fontsize=16,
  18. rotation=trans_angle, rotation_mode='anchor')
  19. plt.show()

下载这个示例