{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Anchored Artists\n\nThis example illustrates the use of the anchored objects without the\nhelper classes found in :mod:`mpl_toolkits.axes_grid1`. This version\nof the figure is similar to the one found in\n:doc:`/gallery/axes_grid1/simple_anchored_artists`, but it is\nimplemented using only the matplotlib namespace, without the help\nof additional toolkits.\n\n.. redirect-from:: /gallery/userdemo/anchored_box01\n.. redirect-from:: /gallery/userdemo/anchored_box02\n.. redirect-from:: /gallery/userdemo/anchored_box03\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\nfrom matplotlib.lines import Line2D\nfrom matplotlib.offsetbox import (AnchoredOffsetbox, AuxTransformBox,\n DrawingArea, TextArea, VPacker)\nfrom matplotlib.patches import Circle, Ellipse\n\n\ndef draw_text(ax):\n \"\"\"Draw a text-box anchored to the upper-left corner of the figure.\"\"\"\n box = AnchoredOffsetbox(child=TextArea(\"Figure 1a\"),\n loc=\"upper left\", frameon=True)\n box.patch.set_boxstyle(\"round,pad=0.,rounding_size=0.2\")\n ax.add_artist(box)\n\n\ndef draw_circles(ax):\n \"\"\"Draw circles in axes coordinates.\"\"\"\n area = DrawingArea(width=40, height=20)\n area.add_artist(Circle((10, 10), 10, fc=\"tab:blue\"))\n area.add_artist(Circle((30, 10), 5, fc=\"tab:red\"))\n box = AnchoredOffsetbox(\n child=area, loc=\"upper right\", pad=0, frameon=False)\n ax.add_artist(box)\n\n\ndef draw_ellipse(ax):\n \"\"\"Draw an ellipse of width=0.1, height=0.15 in data coordinates.\"\"\"\n aux_tr_box = AuxTransformBox(ax.transData)\n aux_tr_box.add_artist(Ellipse((0, 0), width=0.1, height=0.15))\n box = AnchoredOffsetbox(child=aux_tr_box, loc=\"lower left\", frameon=True)\n ax.add_artist(box)\n\n\ndef draw_sizebar(ax):\n \"\"\"\n Draw a horizontal bar with length of 0.1 in data coordinates,\n with a fixed label center-aligned underneath.\n \"\"\"\n size = 0.1\n text = r\"1$^{\\prime}$\"\n sizebar = AuxTransformBox(ax.transData)\n sizebar.add_artist(Line2D([0, size], [0, 0], color=\"black\"))\n text = TextArea(text)\n packer = VPacker(\n children=[sizebar, text], align=\"center\", sep=5) # separation in points.\n ax.add_artist(AnchoredOffsetbox(\n child=packer, loc=\"lower center\", frameon=False,\n pad=0.1, borderpad=0.5)) # paddings relative to the legend fontsize.\n\n\nfig, ax = plt.subplots()\nax.set_aspect(1)\n\ndraw_text(ax)\ndraw_circles(ax)\ndraw_ellipse(ax)\ndraw_sizebar(ax)\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }