简单锚定艺术家对象示例

此示例说明如何使用在 offsetboxMatplotlib axes_grid1 Toolkit 中找到的锚定辅助对象类。类似图形的实现,但不使用工具包,可以在锚定的艺术家对象中找到。

简单锚定艺术家对象示例

  1. import matplotlib.pyplot as plt
  2. def draw_text(ax):
  3. """
  4. Draw two text-boxes, anchored by different corners to the upper-left
  5. corner of the figure.
  6. """
  7. from matplotlib.offsetbox import AnchoredText
  8. at = AnchoredText("Figure 1a",
  9. loc='upper left', prop=dict(size=8), frameon=True,
  10. )
  11. at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
  12. ax.add_artist(at)
  13. at2 = AnchoredText("Figure 1(b)",
  14. loc='lower left', prop=dict(size=8), frameon=True,
  15. bbox_to_anchor=(0., 1.),
  16. bbox_transform=ax.transAxes
  17. )
  18. at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
  19. ax.add_artist(at2)
  20. def draw_circle(ax):
  21. """
  22. Draw a circle in axis coordinates
  23. """
  24. from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea
  25. from matplotlib.patches import Circle
  26. ada = AnchoredDrawingArea(20, 20, 0, 0,
  27. loc='upper right', pad=0., frameon=False)
  28. p = Circle((10, 10), 10)
  29. ada.da.add_artist(p)
  30. ax.add_artist(ada)
  31. def draw_ellipse(ax):
  32. """
  33. Draw an ellipse of width=0.1, height=0.15 in data coordinates
  34. """
  35. from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
  36. ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
  37. loc='lower left', pad=0.5, borderpad=0.4,
  38. frameon=True)
  39. ax.add_artist(ae)
  40. def draw_sizebar(ax):
  41. """
  42. Draw a horizontal bar with length of 0.1 in data coordinates,
  43. with a fixed label underneath.
  44. """
  45. from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
  46. asb = AnchoredSizeBar(ax.transData,
  47. 0.1,
  48. r"1$^{\prime}$",
  49. loc='lower center',
  50. pad=0.1, borderpad=0.5, sep=5,
  51. frameon=False)
  52. ax.add_artist(asb)
  53. ax = plt.gca()
  54. ax.set_aspect(1.)
  55. draw_text(ax)
  56. draw_circle(ax)
  57. draw_ellipse(ax)
  58. draw_sizebar(ax)
  59. plt.show()

下载这个示例