{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# transforms.offset_copy\n\nThis illustrates the use of `.transforms.offset_copy` to\nmake a transform that positions a drawing element such as\na text string at a specified offset in screen coordinates\n(dots or inches) relative to a location given in any\ncoordinates.\n\nEvery Artist (Text, Line2D, etc.) has a transform that can be\nset when the Artist is created, such as by the corresponding\npyplot function. By default, this is usually the Axes.transData\ntransform, going from data units to screen pixels. We can\nuse the `.offset_copy` function to make a modified copy of\nthis transform, where the modification consists of an\noffset.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.transforms as mtransforms\n\nxs = np.arange(7)\nys = xs**2\n\nfig = plt.figure(figsize=(5, 10))\nax = plt.subplot(2, 1, 1)\n\n# If we want the same offset for each text instance,\n# we only need to make one transform. To get the\n# transform argument to offset_copy, we need to make the Axes\n# first; the subplot function above is one way to do this.\ntrans_offset = mtransforms.offset_copy(ax.transData, fig=fig,\n x=0.05, y=0.10, units='inches')\n\nfor x, y in zip(xs, ys):\n plt.plot(x, y, 'ro')\n plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset)\n\n\n# offset_copy works for polar plots also.\nax = plt.subplot(2, 1, 2, projection='polar')\n\ntrans_offset = mtransforms.offset_copy(ax.transData, fig=fig,\n y=6, units='dots')\n\nfor x, y in zip(xs, ys):\n plt.polar(x, y, 'ro')\n plt.text(x, y, '%d, %d' % (int(x), int(y)),\n transform=trans_offset,\n horizontalalignment='center',\n verticalalignment='bottom')\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 }