|
9 | 9 | :doc:`/gallery/axes_grid1/simple_anchored_artists`, but it is
|
10 | 10 | implemented using only the matplotlib namespace, without the help
|
11 | 11 | of additional toolkits.
|
| 12 | +
|
| 13 | +.. redirect-from:: /gallery/userdemo/anchored_box01 |
| 14 | +.. redirect-from:: /gallery/userdemo/anchored_box02 |
| 15 | +.. redirect-from:: /gallery/userdemo/anchored_box03 |
12 | 16 | """
|
13 | 17 |
|
14 | 18 | from matplotlib import pyplot as plt
|
15 | 19 | from matplotlib.lines import Line2D
|
16 |
| -from matplotlib.patches import Ellipse |
| 20 | +from matplotlib.patches import Circle, Ellipse |
17 | 21 | from matplotlib.offsetbox import (
|
18 | 22 | AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
|
19 | 23 |
|
20 | 24 |
|
21 |
| -class AnchoredText(AnchoredOffsetbox): |
22 |
| - def __init__(self, s, loc, pad=0.4, borderpad=0.5, |
23 |
| - prop=None, frameon=True): |
24 |
| - self.txt = TextArea(s) |
25 |
| - super().__init__(loc, pad=pad, borderpad=borderpad, |
26 |
| - child=self.txt, prop=prop, frameon=frameon) |
27 |
| - |
28 |
| - |
29 | 25 | def draw_text(ax):
|
30 |
| - """ |
31 |
| - Draw a text-box anchored to the upper-left corner of the figure. |
32 |
| - """ |
33 |
| - at = AnchoredText("Figure 1a", loc='upper left', frameon=True) |
34 |
| - at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") |
35 |
| - ax.add_artist(at) |
36 |
| - |
37 |
| - |
38 |
| -class AnchoredDrawingArea(AnchoredOffsetbox): |
39 |
| - def __init__(self, width, height, xdescent, ydescent, |
40 |
| - loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): |
41 |
| - self.da = DrawingArea(width, height, xdescent, ydescent) |
42 |
| - super().__init__(loc, pad=pad, borderpad=borderpad, |
43 |
| - child=self.da, prop=None, frameon=frameon) |
44 |
| - |
45 |
| - |
46 |
| -def draw_circle(ax): |
47 |
| - """ |
48 |
| - Draw a circle in axis coordinates |
49 |
| - """ |
50 |
| - from matplotlib.patches import Circle |
51 |
| - ada = AnchoredDrawingArea(20, 20, 0, 0, |
52 |
| - loc='upper right', pad=0., frameon=False) |
53 |
| - p = Circle((10, 10), 10) |
54 |
| - ada.da.add_artist(p) |
55 |
| - ax.add_artist(ada) |
| 26 | + """Draw a text-box anchored to the upper-left corner of the figure.""" |
| 27 | + box = AnchoredOffsetbox(child=TextArea("Figure 1a"), |
| 28 | + loc="upper left", frameon=True) |
| 29 | + box.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") |
| 30 | + ax.add_artist(box) |
56 | 31 |
|
57 | 32 |
|
58 |
| -class AnchoredEllipse(AnchoredOffsetbox): |
59 |
| - def __init__(self, transform, width, height, angle, loc, |
60 |
| - pad=0.1, borderpad=0.1, prop=None, frameon=True): |
61 |
| - """ |
62 |
| - Draw an ellipse the size in data coordinate of the give axes. |
63 |
| -
|
64 |
| - pad, borderpad in fraction of the legend font size (or prop) |
65 |
| - """ |
66 |
| - self._box = AuxTransformBox(transform) |
67 |
| - self.ellipse = Ellipse((0, 0), width, height, angle) |
68 |
| - self._box.add_artist(self.ellipse) |
69 |
| - super().__init__(loc, pad=pad, borderpad=borderpad, |
70 |
| - child=self._box, prop=prop, frameon=frameon) |
| 33 | +def draw_circles(ax): |
| 34 | + """Draw circles in axes coordinates.""" |
| 35 | + area = DrawingArea(40, 20, 0, 0) |
| 36 | + area.add_artist(Circle((10, 10), 10, fc="tab:blue")) |
| 37 | + area.add_artist(Circle((30, 10), 5, fc="tab:red")) |
| 38 | + box = AnchoredOffsetbox( |
| 39 | + child=area, loc="upper right", pad=0, frameon=False) |
| 40 | + ax.add_artist(box) |
71 | 41 |
|
72 | 42 |
|
73 | 43 | def draw_ellipse(ax):
|
74 |
| - """ |
75 |
| - Draw an ellipse of width=0.1, height=0.15 in data coordinates |
76 |
| - """ |
77 |
| - ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., |
78 |
| - loc='lower left', pad=0.5, borderpad=0.4, |
79 |
| - frameon=True) |
80 |
| - |
81 |
| - ax.add_artist(ae) |
| 44 | + """Draw an ellipse of width=0.1, height=0.15 in data coordinates.""" |
| 45 | + aux_tr_box = AuxTransformBox(ax.transData) |
| 46 | + aux_tr_box.add_artist(Ellipse((0, 0), width=0.1, height=0.15)) |
| 47 | + box = AnchoredOffsetbox(child=aux_tr_box, loc="lower left", frameon=True) |
| 48 | + ax.add_artist(box) |
82 | 49 |
|
83 | 50 |
|
84 | 51 | class AnchoredSizeBar(AnchoredOffsetbox):
|
@@ -115,11 +82,11 @@ def draw_sizebar(ax):
|
115 | 82 | ax.add_artist(asb)
|
116 | 83 |
|
117 | 84 |
|
118 |
| -ax = plt.gca() |
119 |
| -ax.set_aspect(1.) |
| 85 | +fig, ax = plt.subplots() |
| 86 | +ax.set_aspect(1) |
120 | 87 |
|
121 | 88 | draw_text(ax)
|
122 |
| -draw_circle(ax) |
| 89 | +draw_circles(ax) |
123 | 90 | draw_ellipse(ax)
|
124 | 91 | draw_sizebar(ax)
|
125 | 92 |
|
|
0 commit comments