{ "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 dowloading 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": { "deletable": true, "editable": true }, "source": [ "#### Version Check\n", "Note: 2D Density Plots are available in version 2.0.0+
\n", "Run `pip install plotly --upgrade` to update your Plotly version" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "'2.0.2'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### 2D Histogram Contour Plot with Histogram Subplots" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "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", "t = np.linspace(-1, 1.2, 2000)\n", "x = (t**3) + (0.3 * np.random.randn(2000))\n", "y = (t**6) + (0.3 * np.random.randn(2000))\n", "\n", "colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)', (1, 1, 0.2), (0.98,0.98,0.98)]\n", "\n", "fig = ff.create_2d_density(\n", " x, y, colorscale=colorscale,\n", " hist_color='rgb(255, 237, 222)', point_size=3\n", ")\n", "\n", "py.iplot(fig, filename='histogram_subplots')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### 2D Histogram Contour Plot with Slider Control" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "https://jsfiddle.net/plotlygraphs/y9sdy76h/4/embedded/result,js,html/" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "deletable": true, "editable": true }, "source": [ "Add slider controls to 2d-density-plot plots with the postMessage API.\n", "\n", "See the code on JSFiddle.\n", "\n", "Watch the 5 second video of how it works:" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "![IPython terminal](https://raw.githubusercontent.com/plotly/documentation/gh-pages/all_static/images/flight_conflicts.gif)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Reference" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function create_2d_density in module plotly.figure_factory._2d_density:\n", "\n", "create_2d_density(x, y, colorscale='Earth', ncontours=20, hist_color=(0, 0, 0.5), point_color=(0, 0, 0.5), point_size=2, title='2D Density Plot', height=600, width=600)\n", " Returns figure for a 2D density plot\n", " \n", " :param (list|array) x: x-axis data for plot generation\n", " :param (list|array) y: y-axis data for plot generation\n", " :param (str|tuple|list) colorscale: either a plotly scale name, an rgb\n", " or hex color, a color tuple or a list or tuple of colors. An rgb\n", " color is of the form 'rgb(x, y, z)' where x, y, z belong to the\n", " interval [0, 255] and a color tuple is a tuple of the form\n", " (a, b, c) where a, b and c belong to [0, 1]. If colormap is a\n", " list, it must contain the valid color types aforementioned as its\n", " members.\n", " :param (int) ncontours: the number of 2D contours to draw on the plot\n", " :param (str) hist_color: the color of the plotted histograms\n", " :param (str) point_color: the color of the scatter points\n", " :param (str) point_size: the color of the scatter points\n", " :param (str) title: set the title for the plot\n", " :param (float) height: the height of the chart\n", " :param (float) width: the width of the chart\n", " \n", " Example 1: Simple 2D Density Plot\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory create_2d_density\n", " \n", " import numpy as np\n", " \n", " # Make data points\n", " t = np.linspace(-1,1.2,2000)\n", " x = (t**3)+(0.3*np.random.randn(2000))\n", " y = (t**6)+(0.3*np.random.randn(2000))\n", " \n", " # Create a figure\n", " fig = create_2D_density(x, y)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='simple-2d-density')\n", " ```\n", " \n", " Example 2: Using Parameters\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory create_2d_density\n", " \n", " import numpy as np\n", " \n", " # Make data points\n", " t = np.linspace(-1,1.2,2000)\n", " x = (t**3)+(0.3*np.random.randn(2000))\n", " y = (t**6)+(0.3*np.random.randn(2000))\n", " \n", " # Create custom colorscale\n", " colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',\n", " (1, 1, 0.2), (0.98,0.98,0.98)]\n", " \n", " # Create a figure\n", " fig = create_2D_density(\n", " x, y, colorscale=colorscale,\n", " hist_color='rgb(255, 237, 222)', point_size=3)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='use-parameters')\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_2d_density)" ] }, { "cell_type": "code", "execution_count": 11, "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", " 'density-plots.ipynb', 'python/density-plots/', 'Python 2d Density Plots | plotly',\n", " 'How to make a 2d density plot in python. Examples of density plots with kernel density estimations, custom color-scales, and smoothing.',\n", " title='Python 2d Density Plots | plotly',\n", " name='2d Density Plots',\n", " thumbnail='thumbnail/density.gif', language='python',\n", " has_thumbnail='true', display_as='statistical', order=7,\n", " ipynb= '~notebook_demo/25')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "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 }