{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!\n", "#### Version Check\n", "Plotly's python API is updated frequesntly. Run pip install plotly --upgrade to update your Plotly version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "'2.0.2'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Basic Quiver Plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "import numpy as np\n", "\n", "x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))\n", "u = np.cos(x)*y\n", "v = np.sin(x)*y\n", "\n", "fig = ff.create_quiver(x, y, u, v)\n", "py.iplot(fig, filename='Quiver Plot Example')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Quiver Plot with Points" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "x,y = np.meshgrid(np.arange(-2, 2, .2),\n", " np.arange(-2, 2, .25))\n", "z = x*np.exp(-x**2 - y**2)\n", "v, u = np.gradient(z, .2, .2)\n", "\n", "# Create quiver figure\n", "fig = ff.create_quiver(x, y, u, v,\n", " scale=.25,\n", " arrow_scale=.4,\n", " name='quiver',\n", " line=dict(width=1))\n", "\n", "# Create points\n", "points = go.Scatter(x=[-.7, .75], y=[0,0],\n", " mode='markers',\n", " marker=dict(size=12),\n", " name='points')\n", "\n", "# Add points to figure\n", "fig['data'].append(points)\n", "\n", "py.iplot(fig, filename='Quiver with Points')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Reference " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function create_quiver in module plotly.figure_factory._quiver:\n", "\n", "create_quiver(x, y, u, v, scale=0.1, arrow_scale=0.3, angle=0.3490658503988659, **kwargs)\n", " Returns data for a quiver plot.\n", " \n", " :param (list|ndarray) x: x coordinates of the arrow locations\n", " :param (list|ndarray) y: y coordinates of the arrow locations\n", " :param (list|ndarray) u: x components of the arrow vectors\n", " :param (list|ndarray) v: y components of the arrow vectors\n", " :param (float in [0,1]) scale: scales size of the arrows(ideally to\n", " avoid overlap). Default = .1\n", " :param (float in [0,1]) arrow_scale: value multiplied to length of barb\n", " to get length of arrowhead. Default = .3\n", " :param (angle in radians) angle: angle of arrowhead. Default = pi/9\n", " :param kwargs: kwargs passed through plotly.graph_objs.Scatter\n", " for more information on valid kwargs call\n", " help(plotly.graph_objs.Scatter)\n", " \n", " :rtype (dict): returns a representation of quiver figure.\n", " \n", " Example 1: Trivial Quiver\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_quiver\n", " \n", " import math\n", " \n", " # 1 Arrow from (0,0) to (1,1)\n", " fig = create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1)\n", " \n", " py.plot(fig, filename='quiver')\n", " ```\n", " \n", " Example 2: Quiver plot using meshgrid\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_quiver\n", " \n", " import numpy as np\n", " import math\n", " \n", " # Add data\n", " x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))\n", " u = np.cos(x)*y\n", " v = np.sin(x)*y\n", " \n", " #Create quiver\n", " fig = create_quiver(x, y, u, v)\n", " \n", " # Plot\n", " py.plot(fig, filename='quiver')\n", " ```\n", " \n", " Example 3: Styling the quiver plot\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_quiver\n", " import numpy as np\n", " import math\n", " \n", " # Add data\n", " x, y = np.meshgrid(np.arange(-np.pi, math.pi, .5),\n", " np.arange(-math.pi, math.pi, .5))\n", " u = np.cos(x)*y\n", " v = np.sin(x)*y\n", " \n", " # Create quiver\n", " fig = create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6,\n", " name='Wind Velocity', line=Line(width=1))\n", " \n", " # Add title to layout\n", " fig['layout'].update(title='Quiver Plot')\n", " \n", " # Plot\n", " py.plot(fig, filename='quiver')\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_quiver)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n", "\n", "/usr/local/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'quiver.ipynb', 'python/quiver-plots/', 'Python Quiver Plots | plotly',\n", " 'How to make a quiver plot in Python. A quiver plot displays velocity vectors a arrows. ',\n", " title = 'Python Quiver Plots | plotly',\n", " name = 'Quiver Plots',\n", " has_thumbnail='true', thumbnail='thumbnail/quiver-plot.jpg', \n", " language='python', \n", " display_as='scientific', order=12,\n", " ipynb= '~notebook_demo/42') " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }