{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Set and get properties\n\nThe pyplot interface allows you to use ``setp`` and ``getp`` to\nset and get object properties respectively, as well as to do\nintrospection on the object.\n\n## Setting with ``setp``\n\nTo set the linestyle of a line to be dashed, you use ``setp``::\n\n >>> line, = plt.plot([1, 2, 3])\n >>> plt.setp(line, linestyle='--')\n\nIf you want to know the valid types of arguments, you can provide the\nname of the property you want to set without a value::\n\n >>> plt.setp(line, 'linestyle')\n linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}\n\nIf you want to see all the properties that can be set, and their\npossible values, you can do::\n\n >>> plt.setp(line)\n\n``setp`` operates on a single instance or a list of instances. If you\nare in query mode introspecting the possible values, only the first\ninstance in the sequence is used. When actually setting values, all\nthe instances will be set. For example, suppose you have a list of\ntwo lines, the following will make both lines thicker and red::\n\n >>> x = np.arange(0, 1, 0.01)\n >>> y1 = np.sin(2*np.pi*x)\n >>> y2 = np.sin(4*np.pi*x)\n >>> lines = plt.plot(x, y1, x, y2)\n >>> plt.setp(lines, linewidth=2, color='r')\n\n\n## Getting with ``getp``\n\n``getp`` returns the value of a given attribute. You can use it to query\nthe value of a single attribute::\n\n >>> plt.getp(line, 'linewidth')\n 0.5\n\nor all the attribute/value pairs::\n\n >>> plt.getp(line)\n aa = True\n alpha = 1.0\n antialiased = True\n c = b\n clip_on = True\n color = b\n ... long listing skipped ...\n\n## Aliases\n\nTo reduce keystrokes in interactive mode, a number of properties\nhave short aliases, e.g., 'lw' for 'linewidth' and 'mec' for\n'markeredgecolor'. When calling set or get in introspection mode,\nthese properties will be listed as 'fullname' or 'aliasname'.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.arange(0, 1.0, 0.01)\ny1 = np.sin(2*np.pi*x)\ny2 = np.sin(4*np.pi*x)\nlines = plt.plot(x, y1, x, y2)\nl1, l2 = lines\nplt.setp(lines, linestyle='--') # set both to dashed\nplt.setp(l1, linewidth=2, color='r') # line1 is thick and red\nplt.setp(l2, linewidth=1, color='g') # line2 is thinner and green\n\n\nprint('Line setters')\nplt.setp(l1)\nprint('Line getters')\nplt.getp(l1)\n\nprint('Rectangle setters')\nplt.setp(plt.gca().patch)\nprint('Rectangle getters')\nplt.getp(plt.gca().patch)\n\nt = plt.title('Hi mom')\nprint('Text setters')\nplt.setp(t)\nprint('Text getters')\nplt.getp(t)\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 }