{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Placing text boxes\n\nWhen decorating Axes with text boxes, two useful tricks are to place the text\nin axes coordinates (see `transforms_tutorial`),\nso the text doesn't move around with changes in x or y limits. You\ncan also use the ``bbox`` property of text to surround the text with a\n`~matplotlib.patches.Patch` instance -- the ``bbox`` keyword argument takes a\ndictionary with keys that are Patch properties.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(19680801)\n\nfig, ax = plt.subplots()\nx = 30*np.random.randn(10000)\nmu = x.mean()\nmedian = np.median(x)\nsigma = x.std()\ntextstr = '\\n'.join((\n r'$\\mu=%.2f$' % (mu, ),\n r'$\\mathrm{median}=%.2f$' % (median, ),\n r'$\\sigma=%.2f$' % (sigma, )))\n\nax.hist(x, 50)\n# these are matplotlib.patch.Patch properties\nprops = dict(boxstyle='round', facecolor='wheat', alpha=0.5)\n\n# place a text box in upper left in axes coords\nax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,\n verticalalignment='top', bbox=props)\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 }