{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Advanced quiver and quiverkey functions\n\nDemonstrates some more advanced options for `~.axes.Axes.quiver`. For a simple\nexample refer to :doc:`/gallery/images_contours_and_fields/quiver_simple_demo`.\n\nNote: The plot autoscaling does not take into account the arrows, so\nthose on the boundaries may reach out of the picture. This is not an easy\nproblem to solve in a perfectly general way. The recommended workaround is to\nmanually set the Axes limits in such a case.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nX, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))\nU = np.cos(X)\nV = np.sin(Y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig1, ax1 = plt.subplots()\nax1.set_title('Arrows scale with plot width, not view')\nQ = ax1.quiver(X, Y, U, V, units='width')\nqk = ax1.quiverkey(Q, 0.9, 0.9, 2, r'$2 \\frac{m}{s}$', labelpos='E',\n coordinates='figure')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig2, ax2 = plt.subplots()\nax2.set_title(\"pivot='mid'; every third arrow; units='inches'\")\nQ = ax2.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],\n pivot='mid', units='inches')\nqk = ax2.quiverkey(Q, 0.9, 0.9, 1, r'$1 \\frac{m}{s}$', labelpos='E',\n coordinates='figure')\nax2.scatter(X[::3, ::3], Y[::3, ::3], color='r', s=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig3, ax3 = plt.subplots()\nax3.set_title(\"pivot='tip'; scales with x view\")\nM = np.hypot(U, V)\nQ = ax3.quiver(X, Y, U, V, M, units='x', pivot='tip', width=0.022,\n scale=1 / 0.15)\nqk = ax3.quiverkey(Q, 0.9, 0.9, 1, r'$1 \\frac{m}{s}$', labelpos='E',\n coordinates='figure')\nax3.scatter(X, Y, color='0.5', s=1)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.quiver` / `matplotlib.pyplot.quiver`\n - `matplotlib.axes.Axes.quiverkey` / `matplotlib.pyplot.quiverkey`\n\n" ] } ], "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 }