注释文本箭头

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig, ax = plt.subplots(figsize=(5, 5))
  4. ax.set_aspect(1)
  5. x1 = -1 + np.random.randn(100)
  6. y1 = -1 + np.random.randn(100)
  7. x2 = 1. + np.random.randn(100)
  8. y2 = 1. + np.random.randn(100)
  9. ax.scatter(x1, y1, color="r")
  10. ax.scatter(x2, y2, color="g")
  11. bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
  12. ax.text(-2, -2, "Sample A", ha="center", va="center", size=20,
  13. bbox=bbox_props)
  14. ax.text(2, 2, "Sample B", ha="center", va="center", size=20,
  15. bbox=bbox_props)
  16. bbox_props = dict(boxstyle="rarrow", fc=(0.8, 0.9, 0.9), ec="b", lw=2)
  17. t = ax.text(0, 0, "Direction", ha="center", va="center", rotation=45,
  18. size=15,
  19. bbox=bbox_props)
  20. bb = t.get_bbox_patch()
  21. bb.set_boxstyle("rarrow", pad=0.6)
  22. ax.set_xlim(-4, 4)
  23. ax.set_ylim(-4, 4)
  24. plt.show()

下载这个示例