{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Auto-wrap text\n\nMatplotlib can wrap text automatically, but if it's too long, the text will\nstill be displayed slightly outside the boundaries of the axis.\n\nNote: Auto-wrapping does not work together with\n``savefig(..., bbox_inches='tight')``. The 'tight' setting rescales the canvas\nto accommodate all content and happens before wrapping. This affects\n``%matplotlib inline`` in IPython and Jupyter notebooks where the inline\nsetting uses ``bbox_inches='tight'`` by default when saving the image to\nembed.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfig = plt.figure()\nplt.axis((0, 10, 0, 10))\nt = (\"This is a really long string that I'd rather have wrapped so that it \"\n \"doesn't go outside of the figure, but if it's long enough it will go \"\n \"off the top or bottom!\")\nplt.text(4, 1, t, ha='left', rotation=15, wrap=True)\nplt.text(6, 5, t, ha='left', rotation=15, wrap=True)\nplt.text(5, 5, t, ha='right', rotation=-15, wrap=True)\nplt.text(5, 10, t, fontsize=18, style='oblique', ha='center',\n va='top', wrap=True)\nplt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)\nplt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)\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 }