Skip to content

Commit cc4d9ce

Browse files
authored
Merge pull request matplotlib#11093 from madphysicist/anchored-artist-gallery
DOC: Fixup to AnchoredArtist examples in the gallery
2 parents 0d4312d + 6f7388c commit cc4d9ce

File tree

3 files changed

+100
-58
lines changed

3 files changed

+100
-58
lines changed

examples/axes_grid1/simple_anchored_artists.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
Simple Anchored Artists
44
=======================
55
6+
This example illustrates the use of the anchored helper classes found in
7+
:py:mod:`~matplotlib.offsetbox` and in the :ref:`toolkit_axesgrid1-index`.
8+
An implementation of a similar figure, but without use of the toolkit,
9+
can be found in :ref:`sphx_glr_gallery_misc_anchored_artists.py`.
610
"""
11+
712
import matplotlib.pyplot as plt
813

914

1015
def draw_text(ax):
16+
"""
17+
Draw two text-boxes, anchored by different corners to the upper-left
18+
corner of the figure.
19+
"""
1120
from matplotlib.offsetbox import AnchoredText
21+
# loc=2 is equivalent to loc='upper left'
1222
at = AnchoredText("Figure 1a",
1323
loc=2, prop=dict(size=8), frameon=True,
1424
)
1525
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
1626
ax.add_artist(at)
1727

28+
# loc=3 is eqivalent to loc='lower left'
1829
at2 = AnchoredText("Figure 1(b)",
1930
loc=3, prop=dict(size=8), frameon=True,
2031
bbox_to_anchor=(0., 1.),
@@ -24,7 +35,10 @@ def draw_text(ax):
2435
ax.add_artist(at2)
2536

2637

27-
def draw_circle(ax): # circle in the canvas coordinate
38+
def draw_circle(ax):
39+
"""
40+
Draw a circle in axis coordinates
41+
"""
2842
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea
2943
from matplotlib.patches import Circle
3044
ada = AnchoredDrawingArea(20, 20, 0, 0,
@@ -35,18 +49,22 @@ def draw_circle(ax): # circle in the canvas coordinate
3549

3650

3751
def draw_ellipse(ax):
52+
"""
53+
Draw an ellipse of width=0.1, height=0.15 in data coordinates
54+
"""
3855
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse
39-
# draw an ellipse of width=0.1, height=0.15 in the data coordinate
4056
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
4157
loc=3, pad=0.5, borderpad=0.4, frameon=True)
4258

4359
ax.add_artist(ae)
4460

4561

4662
def draw_sizebar(ax):
63+
"""
64+
Draw a horizontal bar with length of 0.1 in data coordinates,
65+
with a fixed label underneath.
66+
"""
4767
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
48-
# draw a horizontal bar with length of 0.1 in Data coordinate
49-
# (ax.transData) with a label underneath.
5068
asb = AnchoredSizeBar(ax.transData,
5169
0.1,
5270
r"1$^{\prime}$",
@@ -56,13 +74,12 @@ def draw_sizebar(ax):
5674
ax.add_artist(asb)
5775

5876

59-
if 1:
60-
ax = plt.gca()
61-
ax.set_aspect(1.)
77+
ax = plt.gca()
78+
ax.set_aspect(1.)
6279

63-
draw_text(ax)
64-
draw_circle(ax)
65-
draw_ellipse(ax)
66-
draw_sizebar(ax)
80+
draw_text(ax)
81+
draw_circle(ax)
82+
draw_ellipse(ax)
83+
draw_sizebar(ax)
6784

68-
plt.show()
85+
plt.show()

examples/misc/anchored_artists.py

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
Anchored Artists
44
================
55
6+
This example illustrates the use of the anchored objects without the
7+
helper classes found in the :ref:`toolkit_axesgrid1-index`. This version
8+
of the figure is similar to the one found in
9+
:ref:`sphx_glr_gallery_axes_grid1_simple_anchored_artists.py`, but it is
10+
implemented using only the matplotlib namespace, without the help
11+
of additional toolkits.
612
"""
713

14+
from matplotlib import pyplot as plt
815
from matplotlib.patches import Rectangle, Ellipse
916
from matplotlib.offsetbox import (
1017
AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
@@ -18,27 +25,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5,
1825
child=self.txt, prop=prop, frameon=frameon)
1926

2027

21-
class AnchoredSizeBar(AnchoredOffsetbox):
22-
def __init__(self, transform, size, label, loc,
23-
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
24-
"""
25-
Draw a horizontal bar with the size in data coordinate of the given
26-
axes. A label will be drawn underneath (center-aligned).
28+
def draw_text(ax):
29+
"""
30+
Draw a text-box anchored to the upper-left corner of the figure.
31+
"""
32+
# loc=2 is equivalent to loc='upper left'
33+
at = AnchoredText("Figure 1a", loc=2, frameon=True)
34+
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
35+
ax.add_artist(at)
2736

28-
pad, borderpad in fraction of the legend font size (or prop)
29-
sep in points.
30-
"""
31-
self.size_bar = AuxTransformBox(transform)
32-
self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))
3337

34-
self.txt_label = TextArea(label, minimumdescent=False)
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)
3544

36-
self._box = VPacker(children=[self.size_bar, self.txt_label],
37-
align="center",
38-
pad=0, sep=sep)
3945

40-
super().__init__(loc, pad=pad, borderpad=borderpad,
41-
child=self._box, prop=prop, frameon=frameon)
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=1, pad=0., frameon=False)
53+
p = Circle((10, 10), 10)
54+
ada.da.add_artist(p)
55+
ax.add_artist(ada)
4256

4357

4458
class AnchoredEllipse(AnchoredOffsetbox):
@@ -56,41 +70,44 @@ def __init__(self, transform, width, height, angle, loc,
5670
child=self._box, prop=prop, frameon=frameon)
5771

5872

59-
class AnchoredDrawingArea(AnchoredOffsetbox):
60-
def __init__(self, width, height, xdescent, ydescent,
61-
loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
62-
self.da = DrawingArea(width, height, xdescent, ydescent)
63-
super().__init__(loc, pad=pad, borderpad=borderpad,
64-
child=self.da, prop=None, frameon=frameon)
73+
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=3, pad=0.5, borderpad=0.4, frameon=True)
6579

80+
ax.add_artist(ae)
6681

67-
if __name__ == "__main__":
6882

69-
import matplotlib.pyplot as plt
83+
class AnchoredSizeBar(AnchoredOffsetbox):
84+
def __init__(self, transform, size, label, loc,
85+
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True):
86+
"""
87+
Draw a horizontal bar with the size in data coordinate of the given
88+
axes. A label will be drawn underneath (center-aligned).
7089
71-
ax = plt.gca()
72-
ax.set_aspect(1.)
90+
pad, borderpad in fraction of the legend font size (or prop)
91+
sep in points.
92+
"""
93+
self.size_bar = AuxTransformBox(transform)
94+
self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0))
7395

74-
at = AnchoredText("Figure 1a",
75-
loc=2, frameon=True)
76-
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
77-
ax.add_artist(at)
96+
self.txt_label = TextArea(label, minimumdescent=False)
7897

79-
from matplotlib.patches import Circle
80-
ada = AnchoredDrawingArea(20, 20, 0, 0,
81-
loc=1, pad=0., frameon=False)
82-
p = Circle((10, 10), 10)
83-
ada.da.add_artist(p)
84-
ax.add_artist(ada)
98+
self._box = VPacker(children=[self.size_bar, self.txt_label],
99+
align="center",
100+
pad=0, sep=sep)
85101

86-
# draw an ellipse of width=0.1, height=0.15 in the data coordinate
87-
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
88-
loc=3, pad=0.5, borderpad=0.4, frameon=True)
102+
super().__init__(loc, pad=pad, borderpad=borderpad,
103+
child=self._box, prop=prop, frameon=frameon)
89104

90-
ax.add_artist(ae)
91105

92-
# draw a horizontal bar with length of 0.1 in Data coordinate
93-
# (ax.transData) with a label underneath.
106+
def draw_sizebar(ax):
107+
"""
108+
Draw a horizontal bar with length of 0.1 in data coordinates,
109+
with a fixed label underneath.
110+
"""
94111
asb = AnchoredSizeBar(ax.transData,
95112
0.1,
96113
r"1$^{\prime}$",
@@ -99,5 +116,13 @@ def __init__(self, width, height, xdescent, ydescent,
99116
frameon=False)
100117
ax.add_artist(asb)
101118

102-
plt.draw()
103-
plt.show()
119+
120+
ax = plt.gca()
121+
ax.set_aspect(1.)
122+
123+
draw_text(ax)
124+
draw_circle(ax)
125+
draw_ellipse(ax)
126+
draw_sizebar(ax)
127+
128+
plt.show()

tutorials/text/annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
341341
from matplotlib.offsetbox import AnchoredText
342342
at = AnchoredText("Figure 1a",
343-
prop=dict(size=8), frameon=True,
343+
prop=dict(size=15), frameon=True,
344344
loc=2,
345345
)
346346
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")

0 commit comments

Comments
 (0)