diff --git a/examples/axes_grid1/simple_anchored_artists.py b/examples/axes_grid1/simple_anchored_artists.py index 3d0527298a4c..4db864d0ee07 100644 --- a/examples/axes_grid1/simple_anchored_artists.py +++ b/examples/axes_grid1/simple_anchored_artists.py @@ -3,18 +3,29 @@ Simple Anchored Artists ======================= +This example illustrates the use of the anchored helper classes found in +:py:mod:`~matplotlib.offsetbox` and in the :ref:`toolkit_axesgrid1-index`. +An implementation of a similar figure, but without use of the toolkit, +can be found in :ref:`sphx_glr_gallery_misc_anchored_artists.py`. """ + import matplotlib.pyplot as plt def draw_text(ax): + """ + Draw two text-boxes, anchored by different corners to the upper-left + corner of the figure. + """ from matplotlib.offsetbox import AnchoredText + # loc=2 is equivalent to loc='upper left' at = AnchoredText("Figure 1a", loc=2, prop=dict(size=8), frameon=True, ) at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") ax.add_artist(at) + # loc=3 is eqivalent to loc='lower left' at2 = AnchoredText("Figure 1(b)", loc=3, prop=dict(size=8), frameon=True, bbox_to_anchor=(0., 1.), @@ -24,7 +35,10 @@ def draw_text(ax): ax.add_artist(at2) -def draw_circle(ax): # circle in the canvas coordinate +def draw_circle(ax): + """ + Draw a circle in axis coordinates + """ from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea from matplotlib.patches import Circle ada = AnchoredDrawingArea(20, 20, 0, 0, @@ -35,8 +49,10 @@ def draw_circle(ax): # circle in the canvas coordinate def draw_ellipse(ax): + """ + Draw an ellipse of width=0.1, height=0.15 in data coordinates + """ from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse - # draw an ellipse of width=0.1, height=0.15 in the data coordinate ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., loc=3, pad=0.5, borderpad=0.4, frameon=True) @@ -44,9 +60,11 @@ def draw_ellipse(ax): def draw_sizebar(ax): + """ + Draw a horizontal bar with length of 0.1 in data coordinates, + with a fixed label underneath. + """ from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar - # draw a horizontal bar with length of 0.1 in Data coordinate - # (ax.transData) with a label underneath. asb = AnchoredSizeBar(ax.transData, 0.1, r"1$^{\prime}$", @@ -56,13 +74,12 @@ def draw_sizebar(ax): ax.add_artist(asb) -if 1: - ax = plt.gca() - ax.set_aspect(1.) +ax = plt.gca() +ax.set_aspect(1.) - draw_text(ax) - draw_circle(ax) - draw_ellipse(ax) - draw_sizebar(ax) +draw_text(ax) +draw_circle(ax) +draw_ellipse(ax) +draw_sizebar(ax) - plt.show() +plt.show() diff --git a/examples/misc/anchored_artists.py b/examples/misc/anchored_artists.py index 8bd9dc7103b6..22f35d312465 100644 --- a/examples/misc/anchored_artists.py +++ b/examples/misc/anchored_artists.py @@ -3,8 +3,15 @@ Anchored Artists ================ +This example illustrates the use of the anchored objects without the +helper classes found in the :ref:`toolkit_axesgrid1-index`. This version +of the figure is similar to the one found in +:ref:`sphx_glr_gallery_axes_grid1_simple_anchored_artists.py`, but it is +implemented using only the matplotlib namespace, without the help +of additional toolkits. """ +from matplotlib import pyplot as plt from matplotlib.patches import Rectangle, Ellipse from matplotlib.offsetbox import ( AnchoredOffsetbox, AuxTransformBox, DrawingArea, TextArea, VPacker) @@ -18,27 +25,34 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5, child=self.txt, prop=prop, frameon=frameon) -class AnchoredSizeBar(AnchoredOffsetbox): - def __init__(self, transform, size, label, loc, - pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): - """ - Draw a horizontal bar with the size in data coordinate of the given - axes. A label will be drawn underneath (center-aligned). +def draw_text(ax): + """ + Draw a text-box anchored to the upper-left corner of the figure. + """ + # loc=2 is equivalent to loc='upper left' + at = AnchoredText("Figure 1a", loc=2, frameon=True) + at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") + ax.add_artist(at) - pad, borderpad in fraction of the legend font size (or prop) - sep in points. - """ - self.size_bar = AuxTransformBox(transform) - self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0)) - self.txt_label = TextArea(label, minimumdescent=False) +class AnchoredDrawingArea(AnchoredOffsetbox): + def __init__(self, width, height, xdescent, ydescent, + loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): + self.da = DrawingArea(width, height, xdescent, ydescent) + super().__init__(loc, pad=pad, borderpad=borderpad, + child=self.da, prop=None, frameon=frameon) - self._box = VPacker(children=[self.size_bar, self.txt_label], - align="center", - pad=0, sep=sep) - super().__init__(loc, pad=pad, borderpad=borderpad, - child=self._box, prop=prop, frameon=frameon) +def draw_circle(ax): + """ + Draw a circle in axis coordinates + """ + from matplotlib.patches import Circle + ada = AnchoredDrawingArea(20, 20, 0, 0, + loc=1, pad=0., frameon=False) + p = Circle((10, 10), 10) + ada.da.add_artist(p) + ax.add_artist(ada) class AnchoredEllipse(AnchoredOffsetbox): @@ -56,41 +70,44 @@ def __init__(self, transform, width, height, angle, loc, child=self._box, prop=prop, frameon=frameon) -class AnchoredDrawingArea(AnchoredOffsetbox): - def __init__(self, width, height, xdescent, ydescent, - loc, pad=0.4, borderpad=0.5, prop=None, frameon=True): - self.da = DrawingArea(width, height, xdescent, ydescent) - super().__init__(loc, pad=pad, borderpad=borderpad, - child=self.da, prop=None, frameon=frameon) +def draw_ellipse(ax): + """ + Draw an ellipse of width=0.1, height=0.15 in data coordinates + """ + ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., + loc=3, pad=0.5, borderpad=0.4, frameon=True) + ax.add_artist(ae) -if __name__ == "__main__": - import matplotlib.pyplot as plt +class AnchoredSizeBar(AnchoredOffsetbox): + def __init__(self, transform, size, label, loc, + pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): + """ + Draw a horizontal bar with the size in data coordinate of the given + axes. A label will be drawn underneath (center-aligned). - ax = plt.gca() - ax.set_aspect(1.) + pad, borderpad in fraction of the legend font size (or prop) + sep in points. + """ + self.size_bar = AuxTransformBox(transform) + self.size_bar.add_artist(Rectangle((0, 0), size, 0, ec="black", lw=1.0)) - at = AnchoredText("Figure 1a", - loc=2, frameon=True) - at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") - ax.add_artist(at) + self.txt_label = TextArea(label, minimumdescent=False) - from matplotlib.patches import Circle - ada = AnchoredDrawingArea(20, 20, 0, 0, - loc=1, pad=0., frameon=False) - p = Circle((10, 10), 10) - ada.da.add_artist(p) - ax.add_artist(ada) + self._box = VPacker(children=[self.size_bar, self.txt_label], + align="center", + pad=0, sep=sep) - # draw an ellipse of width=0.1, height=0.15 in the data coordinate - ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., - loc=3, pad=0.5, borderpad=0.4, frameon=True) + super().__init__(loc, pad=pad, borderpad=borderpad, + child=self._box, prop=prop, frameon=frameon) - ax.add_artist(ae) - # draw a horizontal bar with length of 0.1 in Data coordinate - # (ax.transData) with a label underneath. +def draw_sizebar(ax): + """ + Draw a horizontal bar with length of 0.1 in data coordinates, + with a fixed label underneath. + """ asb = AnchoredSizeBar(ax.transData, 0.1, r"1$^{\prime}$", @@ -99,5 +116,13 @@ def __init__(self, width, height, xdescent, ydescent, frameon=False) ax.add_artist(asb) - plt.draw() - plt.show() + +ax = plt.gca() +ax.set_aspect(1.) + +draw_text(ax) +draw_circle(ax) +draw_ellipse(ax) +draw_sizebar(ax) + +plt.show() diff --git a/tutorials/text/annotations.py b/tutorials/text/annotations.py index 5706e08cdc75..3f549671649f 100644 --- a/tutorials/text/annotations.py +++ b/tutorials/text/annotations.py @@ -340,7 +340,7 @@ from matplotlib.offsetbox import AnchoredText at = AnchoredText("Figure 1a", - prop=dict(size=8), frameon=True, + prop=dict(size=15), frameon=True, loc=2, ) at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")