{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "URL: http://matplotlib.org/examples/pylab_examples/quiver_demo.html\n", "\n", "Most examples work across multiple plotting backends, this example is also available for:\n", "\n", "* [Bokeh - quiver_demo](../bokeh/quiver_demo.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import holoviews as hv\n", "from holoviews import opts\n", "\n", "hv.extension('matplotlib')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xs, ys = np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2)\n", "X, Y = np.meshgrid(xs, ys)\n", "U = np.cos(X)\n", "V = np.sin(Y)\n", "\n", "# Convert to magnitude and angle\n", "mag = np.sqrt(U**2 + V**2)\n", "angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "label = 'Arrows scale with plot width, not view'\n", "\n", "opts.defaults(opts.VectorField(aspect=1.5, fig_size=200))\n", "\n", "vectorfield = hv.VectorField((xs, ys, angle, mag))\n", "vectorfield.relabel(label)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "label = \"pivot='mid'; every third arrow\"\n", "\n", "vf_mid = hv.VectorField((xs[::3], ys[::3], angle[::3, ::3], mag[::3, ::3]))\n", "points = hv.Points((X[::3, ::3].flat, Y[::3, ::3].flat)) \n", "\n", "opts.defaults(opts.Points(color='red'))\n", "\n", "(vf_mid * points).relabel(\"pivot='mid'; every third arrow\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "label = \"pivot='tip'; scales with x view\"\n", "vf_tip = hv.VectorField((xs, ys, angle, mag))\n", "points = hv.Points((X.flat, Y.flat))\n", "\n", "(vf_tip * points).opts(\n", " opts.Points(s=5),\n", " opts.VectorField(color='Magnitude', pivot='tip', magnitude='Magnitude', title=label))" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }