{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Artist tests\n\nTest unit support with each of the Matplotlib primitive artist types.\n\nThe axis handles unit conversions and the artists keep a pointer to their axis\nparent. You must initialize the artists with the axis instance if you want to\nuse them with unit data, or else they will not know how to convert the units\nto scalars.\n\n.. only:: builder_html\n\n This example requires :download:`basic_units.py `\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import random\n\nfrom basic_units import cm, inch\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.collections as collections\nimport matplotlib.lines as lines\nimport matplotlib.patches as patches\nimport matplotlib.text as text\n\nfig, ax = plt.subplots()\nax.xaxis.set_units(cm)\nax.yaxis.set_units(cm)\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\nif 0:\n # test a line collection\n # Not supported at present.\n verts = []\n for i in range(10):\n # a random line segment in inches\n verts.append(zip(*inch*10*np.random.rand(2, random.randint(2, 15))))\n lc = collections.LineCollection(verts, axes=ax)\n ax.add_collection(lc)\n\n# test a plain-ol-line\nline = lines.Line2D([0*cm, 1.5*cm], [0*cm, 2.5*cm],\n lw=2, color='black', axes=ax)\nax.add_line(line)\n\nif 0:\n # test a patch\n # Not supported at present.\n rect = patches.Rectangle((1*cm, 1*cm), width=5*cm, height=2*cm,\n alpha=0.2, axes=ax)\n ax.add_patch(rect)\n\n\nt = text.Text(3*cm, 2.5*cm, 'text label', ha='left', va='bottom', axes=ax)\nax.add_artist(t)\n\nax.set_xlim(-1*cm, 10*cm)\nax.set_ylim(-1*cm, 10*cm)\n# ax.xaxis.set_units(inch)\nax.grid(True)\nax.set_title(\"Artists with units\")\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 }