锚定艺术家对象

这个使用没有辅助类的锚定对象的示例在Matplotlib axes_grid1 Toolkit中找到。此版本的图与Simple Anchored Artists中的版本类似,但它仅使用matplotlib命名空间实现,没有其他工具包的帮助。

锚定艺术家对象示例

  1. from matplotlib import pyplot as plt
  2. from matplotlib.patches import Rectangle, Ellipse
  3. from matplotlib.offsetbox import (
  4. AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
  5. class AnchoredText(AnchoredOffsetbox):
  6. def __init__(self, s, loc, pad=0.4, borderpad=0.5,
  7. prop=None, frameon=True):
  8. self.txt = TextArea(s, minimumdescent=False)
  9. super().__init__(loc, pad=pad, borderpad=borderpad,
  10. child=self.txt, prop=prop, frameon=frameon)
  11. def draw_text(ax):
  12. """
  13. Draw a text-box anchored to the upper-left corner of the figure.
  14. """
  15. at = AnchoredText("Figure 1a", loc='upper left', frameon=True)
  16. at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
  17. ax.add_artist(at)
  18. class AnchoredDrawingArea(AnchoredOffsetbox):
  19. def __init__(self, width, height, xdescent, ydescent,
  20. loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
  21. self.da = DrawingArea(width, height, xdescent, ydescent)
  22. super().__init__(loc, pad=pad, borderpad=borderpad,
  23. child=self.da, prop=None, frameon=frameon)
  24. def draw_circle(ax):
  25. """
  26. Draw a circle in axis coordinates
  27. """
  28. from matplotlib.patches import Circle
  29. ada = AnchoredDrawingArea(20, 20, 0, 0,
  30. loc='upper right', pad=0., frameon=False)
  31. p = Circle((10, 10), 10)
  32. ada.da.add_artist(p)
  33. ax.add_artist(ada)
  34. class AnchoredEllipse(AnchoredOffsetbox):
  35. def __init__(self, transform, width, height, angle, loc,
  36. pad=0.1, borderpad=0.1, prop=None, frameon=True):
  37. """
  38. Draw an ellipse the size in data coordinate of the give axes.
  39. pad, borderpad in fraction of the legend font size (or prop)
  40. """
  41. self._box = AuxTransformBox(transform)
  42. self.ellipse = Ellipse((0, 0), width, height, angle)
  43. self._box.add_artist(self.ellipse)
  44. super().__init__(loc, pad=pad, borderpad=borderpad,
  45. child=self._box, prop=prop, frameon=frameon)
  46. def draw_ellipse(ax):
  47. """
  48. Draw an ellipse of width=0.1, height=0.15 in data coordinates
  49. """
  50. ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
  51. loc='lower left', pad=0.5, borderpad=0.4,
  52. frameon=True)
  53. ax.add_artist(ae)
  54. class AnchoredSizeBar(AnchoredOffsetbox):
  55. def __init__(self, transform, size, label, loc,
  56. pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
  57. """
  58. Draw a horizontal bar with the size in data coordinate of the given
  59. axes. A label will be drawn underneath (center-aligned).
  60. pad, borderpad in fraction of the legend font size (or prop)
  61. sep in points.
  62. """
  63. self.size_bar = AuxTransformBox(transform)
  64. self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))
  65. self.txt_label = TextArea(label, minimumdescent=False)
  66. self._box = VPacker(children=[self.size_bar, self.txt_label],
  67. align="center",
  68. pad=0, sep=sep)
  69. super().__init__(loc, pad=pad, borderpad=borderpad,
  70. child=self._box, prop=prop, frameon=frameon)
  71. def draw_sizebar(ax):
  72. """
  73. Draw a horizontal bar with length of 0.1 in data coordinates,
  74. with a fixed label underneath.
  75. """
  76. asb = AnchoredSizeBar(ax.transData,
  77. 0.1,
  78. r"1$^{\prime}$",
  79. loc='lower center',
  80. pad=0.1, borderpad=0.5, sep=5,
  81. frameon=False)
  82. ax.add_artist(asb)
  83. ax = plt.gca()
  84. ax.set_aspect(1.)
  85. draw_text(ax)
  86. draw_circle(ax)
  87. draw_ellipse(ax)
  88. draw_sizebar(ax)
  89. plt.show()

下载这个示例