{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"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": [
"### Basic Contour Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]]\n",
" )\n",
"]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setting X and Y Coordinates in a Contour Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" x=[-9, -6, -5 , -3, -1],\n",
" y=[0, 1, 4, 5, 7]\n",
" )]\n",
"\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Colorscale for Contour Plot ###"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorscale='Jet',\n",
" )]\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Size and Range of a Contour Plot's Contours ###"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorscale='Jet',\n",
" autocontour=False,\n",
" contours=dict(\n",
" start=0,\n",
" end=8,\n",
" size=2,\n",
" ),\n",
" )\n",
"]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Spacing Between X and Y Axis Ticks ###"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" {\n",
" 'z': [[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" 'colorscale':'Jet',\n",
" 'type': u'contour',\n",
" 'dx': 10,\n",
" 'x0': 5,\n",
" 'dy': 10,\n",
" 'y0':10,\n",
" }\n",
"]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Connect the Gaps Between None Values in the Z Matrix ###"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the format of your plot grid:\n",
"[ (1,1) x1,y1 ] [ (1,2) x2,y2 ]\n",
"[ (2,1) x3,y3 ] [ (2,2) x4,y4 ]\n",
"\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.tools as tls\n",
"\n",
"trace0 = {\n",
" 'z': [[None, None, None, 12, 13, 14, 15, 16],\n",
" [None, 1, None, 11, None, None, None, 17],\n",
" [None, 2, 6, 7, None, None, None, 18],\n",
" [None, 3, None, 8, None, None, None, 19],\n",
" [5, 4, 10, 9, None, None, None, 20],\n",
" [None, None, None, 27, None, None, None, 21],\n",
" [None, None, None, 26, 25, 24, 23, 22]],\n",
" 'type': 'contour',\n",
" 'showscale': False\n",
"}\n",
"trace1 = {\n",
" 'z': [[None, None, None, 12, 13, 14, 15, 16],\n",
" [None, 1, None, 11, None, None, None, 17],\n",
" [None, 2, 6, 7, None, None, None, 18],\n",
" [None, 3, None, 8, None, None, None, 19],\n",
" [5, 4, 10, 9, None, None, None, 20],\n",
" [None, None, None, 27, None, None, None, 21],\n",
" [None, None, None, 26, 25, 24, 23, 22]],\n",
" 'connectgaps': True,\n",
" 'type': 'contour',\n",
" 'showscale': False\n",
"}\n",
"trace2 = {\n",
" 'z': [[None, None, None, 12, 13, 14, 15, 16],\n",
" [None, 1, None, 11, None, None, None, 17],\n",
" [None, 2, 6, 7, None, None, None, 18],\n",
" [None, 3, None, 8, None, None, None, 19],\n",
" [5, 4, 10, 9, None, None, None, 20],\n",
" [None, None, None, 27, None, None, None, 21],\n",
" [None, None, None, 26, 25, 24, 23, 22]],\n",
" 'type': 'heatmap',\n",
" 'zsmooth': 'best',\n",
" 'showscale': False\n",
"}\n",
"trace3 = {\n",
" 'z': [[None, None, None, 12, 13, 14, 15, 16],\n",
" [None, 1, None, 11, None, None, None, 17],\n",
" [None, 2, 6, 7, None, None, None, 18],\n",
" [None, 3, None, 8, None, None, None, 19],\n",
" [5, 4, 10, 9, None, None, None, 20],\n",
" [None, None, None, 27, None, None, None, 21],\n",
" [None, None, None, 26, 25, 24, 23, 22]],\n",
" 'connectgaps': True,\n",
" 'type': 'heatmap',\n",
" 'zsmooth': 'best',\n",
" 'showscale': False\n",
"}\n",
"\n",
"fig = tls.make_subplots(rows=2, cols=2, subplot_titles=('connectgaps = False',\n",
" 'connectgaps = True'))\n",
"\n",
"fig.append_trace(trace0, 1, 1)\n",
"fig.append_trace(trace1, 1, 2)\n",
"fig.append_trace(trace2, 2, 1)\n",
"fig.append_trace(trace3, 2, 2)\n",
"\n",
"fig['layout']['yaxis1'].update(title='Contour map')\n",
"fig['layout']['yaxis3'].update(title='Heatmap')\n",
"\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Smoothing the Contour lines ###"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the format of your plot grid:\n",
"[ (1,1) x1,y1 ] [ (1,2) x2,y2 ]\n",
"\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from plotly import tools\n",
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Contour(\n",
" z=[[2, 4, 7, 12, 13, 14, 15, 16],\n",
" [3, 1, 6, 11, 12, 13, 16, 17],\n",
" [4, 2, 7, 7, 11, 14, 17, 18],\n",
" [5, 3, 8, 8, 13, 15, 18, 19],\n",
" [7, 4, 10, 9, 16, 18, 20, 19],\n",
" [9, 10, 5, 27, 23, 21, 21, 21],\n",
" [11, 14, 17, 26, 25, 24, 23, 22]],\n",
" line=dict(smoothing=0),\n",
")\n",
"trace1 = go.Contour(\n",
" z=[[2, 4, 7, 12, 13, 14, 15, 16],\n",
" [3, 1, 6, 11, 12, 13, 16, 17],\n",
" [4, 2, 7, 7, 11, 14, 17, 18],\n",
" [5, 3, 8, 8, 13, 15, 18, 19],\n",
" [7, 4, 10, 9, 16, 18, 20, 19],\n",
" [9, 10, 5, 27, 23, 21, 21, 21],\n",
" [11, 14, 17, 26, 25, 24, 23, 22]],\n",
" line=dict(smoothing=0.85),\n",
")\n",
"\n",
"fig = tools.make_subplots(rows=1, cols=2,\n",
" subplot_titles=('Without Smoothing', 'With Smoothing'))\n",
"\n",
"fig.append_trace(trace0, 1, 1)\n",
"fig.append_trace(trace1, 1, 2)\n",
"\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Smooth Contour Coloring ###"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data=[\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" contours=dict(\n",
" coloring='heatmap'\n",
" )\n",
" )\n",
"]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contour Line Labels ###"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data=[\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" contours=dict(\n",
" coloring ='heatmap',\n",
" showlabels = True,\n",
" labelfont = dict(\n",
" family = 'Raleway',\n",
" size = 12,\n",
" color = 'white',\n",
" )\n",
" )\n",
" )\n",
"]\n",
"\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contour Lines ###"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorscale='Jet',\n",
" contours=dict(\n",
" coloring='lines',\n",
" ),\n",
" )\n",
"]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Custom Contour Plot Colorscale ###"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorscale=[[0, 'rgb(166,206,227)'], [0.25, 'rgb(31,120,180)'], [0.45, 'rgb(178,223,138)'], [0.65, 'rgb(51,160,44)'], [0.85, 'rgb(251,154,153)'], [1, 'rgb(227,26,28)']],\n",
" )\n",
"]\n",
"py.iplot(data, filename='contour-custom-colorscale')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color Bar Title ###"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorbar=dict(\n",
" title='Color bar title',\n",
" titleside='right',\n",
" titlefont=dict(\n",
" size=14,\n",
" family='Arial, sans-serif',\n",
" ),\n",
" )\n",
" )\n",
"]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color Bar Size for Contour Plots"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Contour(\n",
" z=[[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" colorbar=dict(\n",
" thickness=25,\n",
" thicknessmode='pixels',\n",
" len=0.9,\n",
" lenmode='fraction',\n",
" outlinewidth=0\n",
" )\n",
" )\n",
"]\n",
"py.iplot(data, filename='contour-custom-colorbar-size')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Styling Color Bar Ticks for Contour Plots"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [{\n",
" 'type': u'contour',\n",
" 'z': [[10, 10.625, 12.5, 15.625, 20],\n",
" [5.625, 6.25, 8.125, 11.25, 15.625],\n",
" [2.5, 3.125, 5., 8.125, 12.5],\n",
" [0.625, 1.25, 3.125, 6.25, 10.625],\n",
" [0, 0.625, 2.5, 5.625, 10]],\n",
" 'colorbar':{\n",
" 'nticks': 10,\n",
" 'ticks': 'outside',\n",
" 'ticklen': 5,\n",
" 'tickwidth': 1,\n",
" 'showticklabels': True,\n",
" 'tickangle': 0,\n",
" 'tickfont': {\n",
" 'size': 12\n",
" },\n",
" }\n",
" }]\n",
"py.iplot(data)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#contour for more information and chart attribute options!\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-ucfco78x\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher: started\n",
" Running setup.py install for publisher: finished with status 'done'\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Python27\\lib\\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",
"C:\\Python27\\lib\\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",
"\n",
"import publisher\n",
"publisher.publish(\n",
" 'contour.ipynb', 'python/contour-plots/', 'Contour Plots',\n",
" 'How to make Contour plots in Python with Plotly.',\n",
" title = 'Contour Plots | plotly',\n",
" name = 'Contour Plots',\n",
" has_thumbnail='true', thumbnail=' thumbnail/contour.jpg',\n",
" language='python', page_type='example_index',\n",
" display_as='scientific', order=2,\n",
" ipynb= '~notebook_demo/185')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": 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.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}