{ "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 Streamline Plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python2.7/site-packages/plotly/figure_factory/_streamline.py:357: RuntimeWarning:\n", "\n", "invalid value encountered in divide\n", "\n" ] }, { "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 = np.linspace(-3, 3, 100)\n", "y = np.linspace(-3, 3, 100)\n", "Y, X = np.meshgrid(x, y)\n", "u = -1 - X**2 + Y\n", "v = 1 + X - Y**2\n", "\n", "# Create streamline figure\n", "fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)\n", "py.iplot(fig, filename='Streamline Plot Example')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Streamline and Source Point Plot" ] }, { "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", "N = 50\n", "x_start, x_end = -2.0, 2.0\n", "y_start, y_end = -1.0, 1.0\n", "x = np.linspace(x_start, x_end, N)\n", "y = np.linspace(y_start, y_end, N)\n", "X, Y = np.meshgrid(x, y)\n", "source_strength = 5.0\n", "x_source, y_source = -1.0, 0.0\n", "\n", "# Compute the velocity field on the mesh grid\n", "u = (source_strength/(2*np.pi) *\n", " (X-x_source)/((X-x_source)**2 + (Y-y_source)**2))\n", "v = (source_strength/(2*np.pi) *\n", " (Y-y_source)/((X-x_source)**2 + (Y-y_source)**2))\n", "\n", "# Create streamline figure\n", "fig = ff.create_streamline(x, y, u, v,\n", " name='streamline')\n", "\n", "# Add source point\n", "source_point = go.Scatter(x=[x_source], y=[y_source],\n", " mode='markers',\n", " marker=go.Marker(size=14),\n", " name='source point')\n", "\n", "# Add source point to figure\n", "fig['data'].append(source_point)\n", "py.iplot(fig, filename='streamline_source')" ] }, { "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_streamline in module plotly.figure_factory._streamline:\n", "\n", "create_streamline(x, y, u, v, density=1, angle=0.3490658503988659, arrow_scale=0.09, **kwargs)\n", " Returns data for a streamline plot.\n", " \n", " :param (list|ndarray) x: 1 dimensional, evenly spaced list or array\n", " :param (list|ndarray) y: 1 dimensional, evenly spaced list or array\n", " :param (ndarray) u: 2 dimensional array\n", " :param (ndarray) v: 2 dimensional array\n", " :param (float|int) density: controls the density of streamlines in\n", " plot. This is multiplied by 30 to scale similiarly to other\n", " available streamline functions such as matplotlib.\n", " Default = 1\n", " :param (angle in radians) angle: angle of arrowhead. Default = pi/9\n", " :param (float in [0,1]) arrow_scale: value to scale length of arrowhead\n", " Default = .09\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 streamline figure.\n", " \n", " Example 1: Plot simple streamline and increase arrow size\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_streamline\n", " \n", " import numpy as np\n", " import math\n", " \n", " # Add data\n", " x = np.linspace(-3, 3, 100)\n", " y = np.linspace(-3, 3, 100)\n", " Y, X = np.meshgrid(x, y)\n", " u = -1 - X**2 + Y\n", " v = 1 + X - Y**2\n", " u = u.T # Transpose\n", " v = v.T # Transpose\n", " \n", " # Create streamline\n", " fig = create_streamline(x, y, u, v, arrow_scale=.1)\n", " \n", " # Plot\n", " py.plot(fig, filename='streamline')\n", " ```\n", " \n", " Example 2: from nbviewer.ipython.org/github/barbagroup/AeroPython\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_streamline\n", " \n", " import numpy as np\n", " import math\n", " \n", " # Add data\n", " N = 50\n", " x_start, x_end = -2.0, 2.0\n", " y_start, y_end = -1.0, 1.0\n", " x = np.linspace(x_start, x_end, N)\n", " y = np.linspace(y_start, y_end, N)\n", " X, Y = np.meshgrid(x, y)\n", " ss = 5.0\n", " x_s, y_s = -1.0, 0.0\n", " \n", " # Compute the velocity field on the mesh grid\n", " u_s = ss/(2*np.pi) * (X-x_s)/((X-x_s)**2 + (Y-y_s)**2)\n", " v_s = ss/(2*np.pi) * (Y-y_s)/((X-x_s)**2 + (Y-y_s)**2)\n", " \n", " # Create streamline\n", " fig = create_streamline(x, y, u_s, v_s, density=2, name='streamline')\n", " \n", " # Add source point\n", " point = Scatter(x=[x_s], y=[y_s], mode='markers',\n", " marker=Marker(size=14), name='source point')\n", " \n", " # Plot\n", " fig['data'].append(point)\n", " py.plot(fig, filename='streamline')\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_streamline)" ] }, { "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" } ], "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", " 'streamline.ipynb', 'python/streamline-plots/', 'Python Streamline Plots | plotly',\n", " 'How to make a streamline plot in Python. A streamline plot displays vector field data. ',\n", " title = 'Python Streamline Plots | plotly',\n", " name = 'Streamline Plots',\n", " has_thumbnail='true', thumbnail='thumbnail/streamline.jpg', \n", " language='python', \n", " display_as='scientific', order=13,\n", " ipynb= '~notebook_demo/43') " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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 }