8000 Cleanup AnchoredOffsetbox-related demos. · matplotlib/matplotlib@e650590 · GitHub
[go: up one dir, main page]

Skip to content

Commit e650590

Browse files
committed
Cleanup AnchoredOffsetbox-related demos.
`userdemo/anchored_box0{1,2,3}` are basically subsets of `misc/anchored_artists`. `anchored_box04` is slightly more sophisticated; we may want to also merge it but in a later step. In `anchored_artists`, no need to introduce new subclasses (whose constructor need to support both the kwargs for AnchoredOffsetbox and the kwargs for the child artist); it is simpler to just create an AnchoredOffsetbox and set whatever child artist we want on it.
1 parent 0877fd7 commit e650590

File tree

5 files changed

+584
-692
lines changed

5 files changed

+584
-692
lines changed

examples/misc/anchored_artists.py

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,42 @@
99
:doc:`/gallery/axes_grid1/simple_anchored_artists`, but it is
1010
implemented using only the matplotlib namespace, without the help
1111
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
1216
"""
1317

1418
from matplotlib import pyplot as plt
1519
from matplotlib.lines import Line2D
16-
from matplotlib.patches import Ellipse
20+
from matplotlib.patches import Circle, Ellipse
1721
from matplotlib.offsetbox import (
1822
AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
1923

2024

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-
2925
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)
5631

5732

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 coordi 8000 nates."""
35+
box = AnchoredOffsetbox(child=DrawingArea(40, 20, 0, 0),
36+
loc="upper right", pad=0., frameon=False)
37+
box.get_child().add_artist(Circle((10, 10), 10, fc="tab:blue"))
38+
box.get_child().add_artist(Circle((30, 10), 5, fc="tab:red"))
39+
ax.add_artist(box)
7140

7241

7342
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)
43+
"""Draw an ellipse of width=0.1, height=0.15 in data coordinates."""
44+
box = AnchoredOffsetbox(child=AuxTransformBox(ax.transData),
45+
loc="lower left", frameon=True)
46+
box.get_child().add_artist(Ellipse((0, 0), width=0.1, height=0.15))
47+
ax.add_artist(box)
8248

8349

8450
class AnchoredSizeBar(AnchoredOffsetbox):
@@ -115,11 +81,11 @@ def draw_sizebar(ax):
11581
ax.add_artist(asb)
11682

11783

118-
ax = plt.gca()
119-
ax.set_aspect(1.)
84+
fig, ax = plt.subplots()
85+
ax.set_aspect(1)
12086

12187
draw_text(ax)
122-
draw_circle(ax)
88+
draw_circles(ax)
12389
draw_ellipse(ax)
12490
draw_sizebar(ax)
12591

examples/userdemo/anchored_box01.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/userdemo/anchored_box02.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/userdemo/anchored_box03.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0