1"""
2================
3Anchored Artists
4================
5
6This example illustrates the use of the anchored objects without the
7helper classes found in the :ref:`toolkit_axesgrid1-index`. This version
8of the figure is similar to the one found in
9:doc:`/gallery/axes_grid1/simple_anchored_artists`, but it is
10implemented using only the matplotlib namespace, without the help
11of additional toolkits.
12"""
13from matplotlib.patches import Rectangle, Ellipse
14
15from matplotlib.offsetbox import AnchoredOffsetbox, AuxTransformBox, VPacker,\
16    TextArea, DrawingArea
17
18
19class AnchoredText(AnchoredOffsetbox):
20    def __init__(self, s, loc, pad=0.4, borderpad=0.5,
21                 prop=None, frameon=True):
22
23        self.txt = TextArea(s,
24                            minimumdescent=False)
25
26        super(AnchoredText, self).__init__(loc, pad=pad, borderpad=borderpad,
27                                           child=self.txt,
28                                           prop=prop,
29                                           frameon=frameon)
30
31
32class AnchoredSizeBar(AnchoredOffsetbox):
33    def __init__(self, transform, size, label, loc,
34                 pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
35        """
36        Draw a horizontal bar with the size in data coordinate of the given
37        axes. A label will be drawn underneath (center-aligned).
38
39        pad, borderpad in fraction of the legend font size (or prop)
40        sep in points.
41        """
42        self.size_bar = AuxTransformBox(transform)
43        self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))
44
45        self.txt_label = TextArea(label, minimumdescent=False)
46
47        self._box = VPacker(children=[self.size_bar, self.txt_label],
48                            align="center",
49                            pad=0, sep=sep)
50
51        AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad,
52                                   child=self._box,
53                                   prop=prop,
54                                   frameon=frameon)
55
56
57class AnchoredEllipse(AnchoredOffsetbox):
58    def __init__(self, transform, width, height, angle, loc,
59                 pad=0.1, borderpad=0.1, prop=None, frameon=True):
60        """
61        Draw an ellipse the size in data coordinate of the give axes.
62
63        pad, borderpad in fraction of the legend font size (or prop)
64        """
65        self._box = AuxTransformBox(transform)
66        self.ellipse = Ellipse((0, 0), width, height, angle)
67        self._box.add_artist(self.ellipse)
68
69        AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad,
70                                   child=self._box,
71                                   prop=prop,
72                                   frameon=frameon)
73
74
75class AnchoredDrawingArea(AnchoredOffsetbox):
76    def __init__(self, width, height, xdescent, ydescent,
77                 loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
78
79        self.da = DrawingArea(width, height, xdescent, ydescent)
80
81        super(AnchoredDrawingArea, self).__init__(loc, pad=pad,
82                                                  borderpad=borderpad,
83                                                  child=self.da,
84                                                  prop=None,
85                                                  frameon=frameon)
86
87
88if __name__ == "__main__":
89
90    import matplotlib.pyplot as plt
91
92    ax = plt.gca()
93    ax.set_aspect(1.)
94
95    at = AnchoredText("Figure 1a",
96                      loc=2, frameon=True)
97    at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
98    ax.add_artist(at)
99
100    from matplotlib.patches import Circle
101    ada = AnchoredDrawingArea(20, 20, 0, 0,
102                              loc=1, pad=0., frameon=False)
103    p = Circle((10, 10), 10)
104    ada.da.add_artist(p)
105    ax.add_artist(ada)
106
107    # draw an ellipse of width=0.1, height=0.15 in the data coordinate
108    ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
109                         loc=3, pad=0.5, borderpad=0.4, frameon=True)
110
111    ax.add_artist(ae)
112
113    # draw a horizontal bar with length of 0.1 in Data coordinate
114    # (ax.transData) with a label underneath.
115    asb = AnchoredSizeBar(ax.transData,
116                          0.1,
117                          r"1$^{\prime}$",
118                          loc=8,
119                          pad=0.1, borderpad=0.5, sep=5,
120                          frameon=False)
121    ax.add_artist(asb)
122
123    plt.draw()
124    plt.show()
125