10000 DOC: Addressed issue #11092 · matplotlib/matplotlib@ef7c672 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef7c672

Browse files
committed
DOC: Addressed issue #11092
No files were delete, instead, a clarifying comment was added to indicate why there are nearly duplicate files sitting around. [ci skip] [skip ci]
1 parent 0722828 commit ef7c672

File tree

2 files changed

+98
-57
lines changed

2 files changed

+98
-57
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+
matplotlib.offsetbox and in the axes_grid1 namespace of the AxesGrid
8+
toolkit. An implementation of a similar figure, but without use of the
9+
toolkit, can be found in examples/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: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
Anchored Artists
44
================
55
6+
This example illustrates the use of the anchored objects without the
7+
helper classes found in the AxesGrid toolbox. This version of the figure
8+
is similar to the one found in examples/axes_grid1/simple_anchored_artists.py,
9+
but it is implemented using only the matplotlib namespace, without the help
10+
of additional toolkits.
611
"""
712

13+
from matplotlib import pyplot as plt
814
from matplotlib.patches import Rectangle, Ellipse
915
from matplotlib.offsetbox import (
1016
AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker)
@@ -18,27 +24,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5,
1824
child=self.txt, prop=prop, frameon=frameon)
1925

2026

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).
27+
def draw_text(ax):
28+
"""
29+
Draw a text-box anchored to the upper-left corner of the figure.
30+
"""
31+
# loc=2 is equivalent to loc='upper left'
32+
at = AnchoredText("Figure 1a", loc=2, frameon=True)
33+
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
34+
ax.add_artist(at)
2735

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))
3336

34-
self.txt_label = TextArea(label, minimumdescent=False)
37+
class AnchoredDrawingArea(AnchoredOffsetbox):
38+
def __init__(self, width, height, xdescent, ydescent,
39+
loc, pad=0.4, borderpad=0.5, prop=None, frameon=True):
40+
self.da = DrawingArea(width, height, xdescent, ydescent)
41+
super().__init__(loc, pad=pad, borderpad=borderpad,
42+
child=self.da, prop=None, frameon=frameon)
3543

36-
self._box = VPacker(children=[self.size_bar, self.txt_ 10000 label],
37-
align="center",
38-
pad=0, sep=sep)
3944

40-
super().__init__(loc, pad=pad, borderpad=borderpad,
41-
child=self._box, prop=prop, frameon=frameon)
45+
def draw_circle(ax):
46+
"""
47+
Draw a circle in axis coordinates
48+
"""
49+
from matplotlib.patches import Circle
50+
ada = AnchoredDrawingArea(20, 20, 0, 0,
51+
loc=1, pad=0., frameon=False)
52+
p = Circle((10, 10), 10)
53+
ada.da.add_artist(p)
54+
ax.add_artist(ada)
4255

4356

4457
class AnchoredEllipse(AnchoredOffsetbox):
@@ -56,41 +69,44 @@ def __init__(self, transform, width, height, angle, loc,
5669
child=self._box, prop=prop, frameon=frameon)
5770

5871

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)
72+
def draw_ellipse(ax):
73+
"""
74+
Draw an ellipse of width=0.1, height=0.15 in data coordinates
75+
"""
76+
ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0.,
77+
loc=3, pad=0.5, borderpad=0.4, frameon=True)
6578

79+
ax.add_artist(ae)
6680

67-
if __name__ == "__main__":
6881

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

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)
95+
self.txt_label = TextArea(label, minimumdescent=False)
7896

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)
97+
self._box = VPacker(children=[self.size_bar, self.txt_label],
98+
align="center",
99+
pad=0, sep=sep)
85100

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)
101+
super().__init__(loc, pad=pad, borderpad=borderpad,
102+
child=self._box, prop=prop, frameon=frameon)
89103

90-
ax.add_artist(ae)
91104

92-
# draw a horizontal bar with length of 0.1 in Data coordinate
93-
# (ax.transData) with a label underneath.
105+
def draw_sizebar(ax):
106+
"""
107+
Draw a horizontal bar with length of 0.1 in data coordinates,
108+
with a fixed label underneath.
109+
"""
94110
asb = AnchoredSizeBar(ax.transData,
95111
0.1,
96112
r"1$^{\prime}$",
@@ -99,5 +115,13 @@ def __init__(self, width, height, xdescent, ydescent,
99115
frameon=False)
100116
ax.add_artist(asb)
101117

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

0 commit comments

Comments
 (0)
0