简单的Annotate01

简单的Annotate01示例

  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as mpatches
  3. fig, axs = plt.subplots(2, 4)
  4. x1, y1 = 0.3, 0.3
  5. x2, y2 = 0.7, 0.7
  6. ax = axs.flat[0]
  7. ax.plot([x1, x2], [y1, y2], "o")
  8. ax.annotate("",
  9. xy=(x1, y1), xycoords='data',
  10. xytext=(x2, y2), textcoords='data',
  11. arrowprops=dict(arrowstyle="->"))
  12. ax.text(.05, .95, "A $->$ B", transform=ax.transAxes, ha="left", va="top")
  13. ax = axs.flat[2]
  14. ax.plot([x1, x2], [y1, y2], "o")
  15. ax.annotate("",
  16. xy=(x1, y1), xycoords='data',
  17. xytext=(x2, y2), textcoords='data',
  18. arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.3",
  19. shrinkB=5)
  20. )
  21. ax.text(.05, .95, "shrinkB=5", transform=ax.transAxes, ha="left", va="top")
  22. ax = axs.flat[3]
  23. ax.plot([x1, x2], [y1, y2], "o")
  24. ax.annotate("",
  25. xy=(x1, y1), xycoords='data',
  26. xytext=(x2, y2), textcoords='data',
  27. arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.3"))
  28. ax.text(.05, .95, "connectionstyle=arc3", transform=ax.transAxes, ha="left", va="top")
  29. ax = axs.flat[4]
  30. ax.plot([x1, x2], [y1, y2], "o")
  31. el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.5)
  32. ax.add_artist(el)
  33. ax.annotate("",
  34. xy=(x1, y1), xycoords='data',
  35. xytext=(x2, y2), textcoords='data',
  36. arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.2")
  37. )
  38. ax = axs.flat[5]
  39. ax.plot([x1, x2], [y1, y2], "o")
  40. el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.5)
  41. ax.add_artist(el)
  42. ax.annotate("",
  43. xy=(x1, y1), xycoords='data',
  44. xytext=(x2, y2), textcoords='data',
  45. arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.2",
  46. patchB=el)
  47. )
  48. ax.text(.05, .95, "patchB", transform=ax.transAxes, ha="left", va="top")
  49. ax = axs.flat[6]
  50. ax.plot([x1], [y1], "o")
  51. ax.annotate("Test",
  52. xy=(x1, y1), xycoords='data',
  53. xytext=(x2, y2), textcoords='data',
  54. ha="center", va="center",
  55. bbox=dict(boxstyle="round", fc="w"),
  56. arrowprops=dict(arrowstyle="->")
  57. )
  58. ax.text(.05, .95, "annotate", transform=ax.transAxes, ha="left", va="top")
  59. ax = axs.flat[7]
  60. ax.plot([x1], [y1], "o")
  61. ax.annotate("Test",
  62. xy=(x1, y1), xycoords='data',
  63. xytext=(x2, y2), textcoords='data',
  64. ha="center", va="center",
  65. bbox=dict(boxstyle="round", fc="w", ),
  66. arrowprops=dict(arrowstyle="->", relpos=(0., 0.))
  67. )
  68. ax.text(.05, .95, "relpos=(0,0)", transform=ax.transAxes, ha="left", va="top")
  69. for ax in axs.flat:
  70. ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
  71. plt.show()

下载这个示例