{ "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Note: 2D Histograms are available in version 1.9.12+
\n", "Run `pip install plotly --upgrade` to update your Plotly version" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "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": {}, "source": [ "### 2D Histogram with Slider Control ###" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add slider controls to 2d histograms with the [postMessage API](https://github.com/plotly/postMessage-API). \n", " See the [code on JSFiddle](https://jsfiddle.net/plotlygraphs/y9sdy76h/4/). \n", " Watch [the 5 second video](https://raw.githubusercontent.com/plotly/documentation/gh-pages/all_static/images \n", "/flight_conflicts.gif) of how it works. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.core.display import display,HTML\n", "display(HTML(''))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D Histogram of a Bivariate Normal Distribution ###" ] }, { "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", "import numpy as np\n", "\n", "x = np.random.randn(500)\n", "y = np.random.randn(500)+1\n", "\n", "data = [\n", " go.Histogram2d(\n", " x=x,\n", " y=y\n", " )\n", "]\n", "py.iplot(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D Histogram Binning and Styling Options ###" ] }, { "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", "import numpy as np\n", "\n", "x = np.random.randn(500)\n", "y = np.random.randn(500)+1\n", "\n", "data = [\n", " go.Histogram2d(x=x, y=y, histnorm='probability',\n", " autobinx=False,\n", " xbins=dict(start=-3, end=3, size=0.1),\n", " autobiny=False,\n", " ybins=dict(start=-2.5, end=4, size=0.1),\n", " colorscale=[[0, 'rgb(12,51,131)'], [0.25, 'rgb(10,136,186)'], [0.5, 'rgb(242,211,56)'], [0.75, 'rgb(242,143,56)'], [1, 'rgb(217,30,30)']]\n", " )\n", "]\n", "py.iplot(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D Histogram Overlaid with a Scatter Chart ###" ] }, { "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", "import numpy as np\n", "\n", "x0 = np.random.randn(100)/5. + 0.5 # 5. enforces float division\n", "y0 = np.random.randn(100)/5. + 0.5\n", "x1 = np.random.rand(50)\n", "y1 = np.random.rand(50) + 1.0\n", "\n", "x = np.concatenate([x0, x1])\n", "y = np.concatenate([y0, y1])\n", "\n", "trace1 = go.Scatter(\n", " x=x0,\n", " y=y0,\n", " mode='markers',\n", " showlegend=False,\n", " marker=dict(\n", " symbol='x',\n", " opacity=0.7,\n", " color='white',\n", " size=8,\n", " line=dict(width=1),\n", " )\n", ")\n", "trace2 = go.Scatter(\n", " x=x1,\n", " y=y1,\n", " mode='markers',\n", " showlegend=False,\n", " marker=dict(\n", " symbol='circle',\n", " opacity=0.7,\n", " color='white',\n", " size=8,\n", " line=dict(width=1),\n", " )\n", ")\n", "trace3 = go.Histogram2d(\n", " x=x,\n", " y=y,\n", " colorscale='YlGnBu',\n", " zmax=10,\n", " nbinsx=14,\n", " nbinsy=14,\n", " zauto=False,\n", ")\n", "\n", "layout = go.Layout(\n", " xaxis=dict( ticks='', showgrid=False, zeroline=False, nticks=20 ),\n", " yaxis=dict( ticks='', showgrid=False, zeroline=False, nticks=20 ),\n", " autosize=False,\n", " height=550,\n", " width=550,\n", " hovermode='closest',\n", "\n", ")\n", "data = [trace1, trace2, trace3]\n", "fig = go.Figure(data=data, layout=layout)\n", "\n", "py.iplot(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#histogram2d for more information and chart attribute options!\n" ] }, { "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-LGAyhO\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-4H5seP/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", " '2d-histograms.ipynb', 'python/2D-Histogram/', 'Python 2D Histograms | plotly',\n", " 'How to make 2D Histograms in Python with Plotly.',\n", " title = 'Python 2D Histograms | plotly',\n", " name = '2D Histograms',\n", " has_thumbnail='true', thumbnail='thumbnail/histogram2d.jpg', \n", " language='python', display_as='statistical', order=6,\n", " ipynb= '~notebook_demo/24') " ] }, { "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }