{ "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!\n", "#### 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": {}, "outputs": [ { "data": { "text/plain": [ "'2.2.2'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Heatmap" ] }, { "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", "trace = go.Heatmap(z=[[1, 20, 30],\n", " [20, 1, 60],\n", " [30, 60, 1]])\n", "data=[trace]\n", "py.iplot(data, filename='basic-heatmap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Heatmap with Categorical Axis Labels" ] }, { "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", "trace = go.Heatmap(z=[[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],\n", " x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],\n", " y=['Morning', 'Afternoon', 'Evening'])\n", "data=[trace]\n", "py.iplot(data, filename='labelled-heatmap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Heatmap with Unequal Block Sizes\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import plotly.plotly as py\n", "\n", "def spiral(th):\n", " a = 1.120529\n", " b = 0.306349\n", " r = a*np.exp(-b*th)\n", " return (r*np.cos(th), r*np.sin(th))\n", "\n", "nspiral = 2 # number of spiral loops\n", "\n", "th = np.linspace(-np.pi/13,2*np.pi*nspiral,1000); # angle\n", "(x,y) = spiral(th)\n", "\n", "# shift the spiral north so that it is centered\n", "yshift = (1.6 - (max(y)-min(y)))/2\n", "\n", "s = dict(x= -x+x[0], y= y-y[0]+yshift,\n", " line =dict(color='white',width=3)) \n", "\n", "# Build the rectangles as a heatmap\n", "# specify the edges of the heatmap squares\n", "phi = ( 1+np.sqrt(5) )/2.\n", "xe = [0, 1, 1+(1/(phi**4)), 1+(1/(phi**3)), phi]\n", "ye = [0, 1/(phi**3),1/phi**3+1/phi**4,1/(phi**2),1]\n", "\n", "z = [ [13,3,3,5],\n", " [13,2,1,5],\n", " [13,10,11,12],\n", " [13,8,8,8]\n", " ]\n", "\n", "hm = dict(x = np.sort(xe),\n", " y = np.sort(ye)+yshift,\n", " z = z,\n", " type = 'heatmap',\n", " colorscale = 'Viridis')\n", "\n", "axis_template = dict(range = [0,1.6], autorange = False,\n", " showgrid = False, zeroline = False,\n", " linecolor = 'black', showticklabels = False,\n", " ticks = '' )\n", "\n", "layout = dict( margin = dict(t=200,r=200,b=200,l=200),\n", " xaxis = axis_template,\n", " yaxis = axis_template,\n", " showlegend = False,\n", " width = 700, height = 700,\n", " autosize = False )\n", "\n", "figure = dict(data=[s, hm],layout=layout)\n", "\n", "py.iplot(figure, filename='golden spiral', height=750)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Heatmap with Datetime Axis" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "import datetime\n", "import numpy as np\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "programmers = ['Alex','Nicole','Sara','Etienne','Chelsea','Jody','Marianne']\n", "\n", "base = datetime.datetime.today()\n", "date_list = [base - datetime.timedelta(days=x) for x in range(0, 180)]\n", "\n", "z = []\n", "\n", "for prgmr in programmers:\n", " new_row = []\n", " for date in date_list:\n", " new_row.append( np.random.poisson() )\n", " z.append(list(new_row))\n", "\n", "data = [\n", " go.Heatmap(\n", " z=z,\n", " x=date_list,\n", " y=programmers,\n", " colorscale='Viridis',\n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " title='GitHub commits per day',\n", " xaxis = dict(ticks='', nticks=36),\n", " yaxis = dict(ticks='' )\n", ")\n", "\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig, filename='datetime-heatmap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dash Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its source code can be found [here](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-heatmapplot) and can easily be deployed to a PaaS." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-heatmapplot/\", width=\"120%\", height=\"650px\", frameBorder=\"0\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(src= \"https://dash-simple-apps.plotly.host/dash-heatmapplot/code\", width=\"120%\", height=500, frameBorder=\"0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#heatmap for more information and chart attribute options!\n" ] }, { "cell_type": "code", "execution_count": 1, "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 /tmp/pip-req-build-s5qxb1jf\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-ugbgjrgp/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 10.0.1, however version 19.1.1 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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", " 'heatmaps.ipynb', ' python/heatmaps/', 'Heatmaps | plotly',\n", " 'How to make Heatmaps in Python with Plotly.',\n", " title = 'Python Heatmaps | plotly',\n", " name = 'Heatmaps',\n", " has_thumbnail='true', thumbnail='thumbnail/heatmap.jpg', \n", " language='python', page_type='example_index',\n", " display_as='scientific',order=3,\n", " ipynb= '~notebook_demo/33', redirect_from='python/heatmap/') " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }