1""" 2======================= 3Simple Anchored Artists 4======================= 5 6This example illustrates the use of the anchored helper classes found in 7:py:mod:`~matplotlib.offsetbox` and in the :ref:`toolkit_axesgrid1-index`. 8An implementation of a similar figure, but without use of the toolkit, 9can be found in :doc:`/gallery/misc/anchored_artists`. 10""" 11import matplotlib.pyplot as plt 12 13 14def draw_text(ax): 15 from matplotlib.offsetbox import AnchoredText 16 at = AnchoredText("Figure 1a", 17 loc=2, prop=dict(size=8), frameon=True, 18 ) 19 at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") 20 ax.add_artist(at) 21 22 at2 = AnchoredText("Figure 1(b)", 23 loc=3, prop=dict(size=8), frameon=True, 24 bbox_to_anchor=(0., 1.), 25 bbox_transform=ax.transAxes 26 ) 27 at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") 28 ax.add_artist(at2) 29 30 31def draw_circle(ax): # circle in the canvas coordinate 32 from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea 33 from matplotlib.patches import Circle 34 ada = AnchoredDrawingArea(20, 20, 0, 0, 35 loc=1, pad=0., frameon=False) 36 p = Circle((10, 10), 10) 37 ada.da.add_artist(p) 38 ax.add_artist(ada) 39 40 41def draw_ellipse(ax): 42 from mpl_toolkits.axes_grid1.anchored_artists import AnchoredEllipse 43 # draw an ellipse of width=0.1, height=0.15 in the data coordinate 44 ae = AnchoredEllipse(ax.transData, width=0.1, height=0.15, angle=0., 45 loc=3, pad=0.5, borderpad=0.4, frameon=True) 46 47 ax.add_artist(ae) 48 49 50def draw_sizebar(ax): 51 from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar 52 # draw a horizontal bar with length of 0.1 in Data coordinate 53 # (ax.transData) with a label underneath. 54 asb = AnchoredSizeBar(ax.transData, 55 0.1, 56 r"1$^{\prime}$", 57 loc=8, 58 pad=0.1, borderpad=0.5, sep=5, 59 frameon=False) 60 ax.add_artist(asb) 61 62 63if 1: 64 ax = plt.gca() 65 ax.set_aspect(1.) 66 67 draw_text(ax) 68 draw_circle(ax) 69 draw_ellipse(ax) 70 draw_sizebar(ax) 71 72 plt.show() 73