注释变换

此示例显示如何使用不同的坐标系进行注释。 有关注释功能的完整概述,另请参阅注释教程

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. x = np.arange(0, 10, 0.005)
  4. y = np.exp(-x/2.) * np.sin(2*np.pi*x)
  5. fig, ax = plt.subplots()
  6. ax.plot(x, y)
  7. ax.set_xlim(0, 10)
  8. ax.set_ylim(-1, 1)
  9. xdata, ydata = 5, 0
  10. xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata))
  11. bbox = dict(boxstyle="round", fc="0.8")
  12. arrowprops = dict(
  13. arrowstyle = "->",
  14. connectionstyle = "angle,angleA=0,angleB=90,rad=10")
  15. offset = 72
  16. ax.annotate('data = (%.1f, %.1f)'%(xdata, ydata),
  17. (xdata, ydata), xytext=(-2*offset, offset), textcoords='offset points',
  18. bbox=bbox, arrowprops=arrowprops)
  19. disp = ax.annotate('display = (%.1f, %.1f)'%(xdisplay, ydisplay),
  20. (xdisplay, ydisplay), xytext=(0.5*offset, -offset),
  21. xycoords='figure pixels',
  22. textcoords='offset points',
  23. bbox=bbox, arrowprops=arrowprops)
  24. plt.show()

注释变换示例

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.transforms.Transform.transform_point
  3. matplotlib.axes.Axes.annotate
  4. matplotlib.pyplot.annotate

下载这个示例