{ "cells": [ { "cell_type": "markdown", "metadata": { "inputHidden": false, "outputHidden": false }, "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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Carpet Plot\n", "\n", "Set the `x` and `y` coorindates, using `x` and `y` attributes. If `x` coorindate values are ommitted a cheater plot will be created. To save parameter values use `a` and `b` attributes. To make changes to the axes, use `aaxis` or `baxis` attributes. For a more detailed list of axes attributes refer to [python reference](https://plotly.com/python/reference/#carpet-aaxis)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.graph_objs as go\n", "import plotly.plotly as py\n", "\n", "trace1 = go.Carpet(\n", " a = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n", " b = [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n", " x = [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],\n", " y = [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],\n", " aaxis = dict(\n", " tickprefix = 'a = ',\n", " smoothing = 0,\n", " minorgridcount = 9,\n", " type = 'linear'\n", " ),\n", " baxis = dict(\n", " tickprefix = 'b = ',\n", " smoothing = 0,\n", " minorgridcount = 9,\n", " type = 'linear'\n", " )\n", ")\n", "\n", "data = [trace1]\n", "\n", "layout = go.Layout(\n", " margin = dict(\n", " \tt = 40,\n", " r = 30,\n", " b = 30,\n", " l = 30\n", " ),\n", " yaxis = dict(\n", " range = [0.388,4.361]\n", " ),\n", " xaxis = dict(\n", " \trange = [0.667,5.932]\t\n", " )\n", ")\n", "\n", "fig = go.Figure(data = data, layout = layout)\n", "py.iplot(fig, filename = \"contourcarpet/basic\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add Contours" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.graph_objs as go\n", "import plotly.plotly as py\n", "\n", "trace1 = go.Contourcarpet(\n", " a = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n", " b = [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n", " z = [1, 1.96, 2.56, 3.0625, 4, 5.0625, 1, 7.5625, 9, 12.25, 15.21, 14.0625],\n", " autocontour = False,\n", " contours = dict(\n", " \tstart = 1,\n", " end = 14,\n", " size = 1\n", " ),\n", " line = dict(\n", " \twidth = 2,\n", " \tsmoothing = 0\n", " ),\n", " colorbar = dict(\n", " \tlen = 0.4,\n", " y = 0.25\n", " )\n", ")\n", "\n", "trace2 = go.Carpet(\n", " a = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n", " b = [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n", " x = [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],\n", " y = [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],\n", " aaxis = dict(\n", " tickprefix = 'a = ',\n", " smoothing = 0,\n", " minorgridcount = 9,\n", " type = 'linear'\n", " ),\n", " baxis = dict(\n", " tickprefix = 'b = ',\n", " smoothing = 0,\n", " minorgridcount = 9,\n", " type = 'linear'\n", " )\n", ")\n", "\n", "data = [trace1, trace2]\n", "\n", "layout = go.Layout(\n", " margin = dict(\n", " \tt = 40,\n", " r = 30,\n", " b = 30,\n", " l = 30\n", " ),\n", " yaxis = dict(\n", " range = [0.388,4.361]\n", " ),\n", " xaxis = dict(\n", " \trange = [0.667,5.932]\t\n", " )\n", ")\n", "\n", "fig = go.Figure(data = data, layout = layout)\n", "py.iplot(fig, filename = \"contourcarpet/add-contours\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add Multiple Traces" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.graph_objs as go\n", "import plotly.plotly as py\n", "\n", "import urllib, json\n", "\n", "url = \"https://raw.githubusercontent.com/bcdunbar/datasets/master/airfoil_data.json\"\n", "response = urllib.urlopen(url)\n", "data = json.loads(response.read())\n", " \n", "trace1 = go.Carpet(\n", " a = data[0]['a'],\n", " b = data[0]['b'],\n", " x = data[0]['x'],\n", " y = data[0]['y'],\n", " baxis = dict(\n", " startline = False,\n", " endline = False,\n", " showticklabels = \"none\",\n", " smoothing = 0,\n", " showgrid = False\n", " ),\n", " aaxis = dict(\n", " startlinewidth = 2,\n", " startline = True,\n", " showticklabels = \"none\",\n", " endline = True,\n", " showgrid = False,\n", " endlinewidth = 2,\n", " smoothing = 0\n", " )\n", ")\n", "\n", "trace2 = go.Contourcarpet(\n", " z = data[1]['z'],\n", " autocolorscale = False,\n", " zmax = 1,\n", " name = \"Pressure\",\n", " colorscale = \"Viridis\",\n", " zmin = -8,\n", " colorbar = dict(\n", " y = 0,\n", " yanchor = \"bottom\",\n", " titleside = \"right\",\n", " len = 0.75,\n", " title = \"Pressure coefficient, cp\"\n", " ),\n", " contours = dict(\n", " start = -1,\n", " size = 0.025,\n", " end = 1.000,\n", " showlines = False\n", " ),\n", " line = dict(\n", " smoothing = 0\n", " ),\n", " autocontour = False,\n", " zauto = False\n", ")\n", "\n", "trace3 = go.Contourcarpet(\n", " z = data[2]['z'],\n", " opacity = 0.300,\n", " showlegend = True,\n", " name = \"Streamlines\",\n", " autocontour = True,\n", " ncontours = 50,\n", " contours = dict(\n", " coloring = \"none\"\n", " ),\n", " line = dict(\n", " color = \"white\",\n", " width = 1\n", " )\n", ")\n", "\n", "trace4 = go.Contourcarpet(\n", " z = data[3]['z'],\n", " showlegend = True,\n", " name = \"Pressure
contours\",\n", " autocontour = False,\n", " line = dict(\n", " color = \"rgba(0, 0, 0, 0.5)\",\n", " smoothing = 1\n", " ),\n", " contours = dict(\n", " size = 0.250,\n", " start = -4,\n", " coloring = \"none\",\n", " end = 1.000,\n", " showlines = True\n", " )\n", ")\n", "\n", "trace5 = go.Scatter(\n", " x = data[4]['x'],\n", " y = data[4]['y'],\n", " legendgroup = \"g1\",\n", " name = \"Surface
pressure\",\n", " mode = \"lines\",\n", " hoverinfo = \"skip\",\n", " line = dict(\n", " color = \"rgba(255, 0, 0, 0.5)\",\n", " width = 1,\n", " shape = \"spline\",\n", " smoothing = 1\n", " ),\n", " fill = \"toself\",\n", " fillcolor = \"rgba(255, 0, 0, 0.2)\"\n", ")\n", "\n", "trace6 = go.Scatter(\n", " x = data[5]['x'],\n", " y = data[5]['y'],\n", " showlegend = False,\n", " legendgroup = \"g1\",\n", " mode = \"lines\",\n", " hoverinfo = \"skip\",\n", " line = dict(\n", " color = \"rgba(255, 0, 0, 0.3)\",\n", " width = 1\n", " )\n", ")\n", "\n", "trace7 = go.Scatter(\n", " x = data[6]['x'],\n", " y = data[6]['y'],\n", " showlegend = False,\n", " legendgroup = \"g1\",\n", " name = \"cp\",\n", " text = data[6]['text'],\n", " hoverinfo = \"text\",\n", " mode = \"lines\",\n", " line = dict(\n", " color = \"rgba(255, 0, 0, 0.2)\",\n", " width = 0\n", " )\n", ")\n", "\n", "data = [trace1,trace2,trace3,trace4,trace5,trace6,trace7]\n", "\n", "layout = go.Layout(\n", " yaxis = dict(\n", " zeroline = False,\n", " range = [-1.800,1.800],\n", " showgrid = False\n", " ),\n", " dragmode = \"pan\",\n", " height = 700,\n", " xaxis = dict(\n", " zeroline = False,\n", " scaleratio = 1,\n", " scaleanchor = 'y',\n", " range = [-3.800,3.800],\n", " showgrid = False\n", " ),\n", " title = \"Flow over a Karman-Trefftz airfoil\",\n", " hovermode = \"closest\",\n", " margin = dict(\n", " r = 60,\n", " b = 40,\n", " l = 40,\n", " t = 80\n", " ),\n", " width = 900\n", ")\n", "\n", "fig = go.Figure(data=data,layout=layout)\n", "py.iplot(fig, filename = \"contourcarpet/airfoil\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See https://plotly.com/python/reference/#contourcarpet for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-kJPbmE\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-ClMTFO/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "/Library/Frameworks/Python.framework/Versions/2.7/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", " 'contourcarpet.ipynb', 'python/carpet-contour/', 'Carpet Contour Plot',\n", " 'How to make carpet contour plots in Python with Plotly.',\n", " title = 'Carpet Contour Plots | Plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/contourcarpet.jpg', \n", " language='python', \n", " # page_type='example_index', // note this is only if you want the tutorial to appear on the main page: plot.ly/python\n", " display_as='scientific', order=27,\n", " ipynb= '~notebook_demo/145')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernel_info": { "name": "python2" }, "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": 4 }