{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", " \n", "___\n", "# Plotly and Cufflinks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotly is a library that allows you to create interactive plots that you can use in dashboards or websites (you can save them as html files or static images).\n", "\n", "## Installation\n", "\n", "In order for this all to work, you'll need to install plotly and cufflinks to call plots directly off of a pandas dataframe. These libraries are not currently available through **conda** but are available through **pip**. Install the libraries at your command line/terminal using:\n", "\n", " pip install plotly\n", " pip install cufflinks\n", "\n", "** NOTE: Make sure you only have one installation of Python on your computer when you do this, otherwise the installation may not work. **\n", "\n", "## Imports and Set-up" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.12.9\n" ] } ], "source": [ "from plotly import __version__\n", "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n", "\n", "print(__version__) # requires version >= 1.9.0" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import cufflinks as cf" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# For Notebooks\n", "init_notebook_mode(connected=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# For offline use\n", "cf.go_offline()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fake Data" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = pd.DataFrame(np.random.randn(100,4),columns='A B C D'.split())" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCD
01.8787250.6887191.0667330.543956
10.0287340.1040540.0481761.842188
2-0.1587930.387926-0.635371-0.637558
3-1.2219721.393423-0.299794-1.113622
41.253152-0.5375980.302917-2.546083
\n", "
" ], "text/plain": [ " A B C D\n", "0 1.878725 0.688719 1.066733 0.543956\n", "1 0.028734 0.104054 0.048176 1.842188\n", "2 -0.158793 0.387926 -0.635371 -0.637558\n", "3 -1.221972 1.393423 -0.299794 -1.113622\n", "4 1.253152 -0.537598 0.302917 -2.546083" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df2 = pd.DataFrame({'Category':['A','B','C'],'Values':[32,43,50]})" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CategoryValues
0A32
1B43
2C50
\n", "
" ], "text/plain": [ " Category Values\n", "0 A 32\n", "1 B 43\n", "2 C 50" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Cufflinks and iplot()\n", "\n", "* scatter\n", "* bar\n", "* box\n", "* spread\n", "* ratio\n", "* heatmap\n", "* surface\n", "* histogram\n", "* bubble" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scatter" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df.iplot(kind='scatter',x='A',y='B',mode='markers',size=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bar Plots" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df2.iplot(kind='bar',x='Category',y='Values')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df.count().iplot(kind='bar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boxplots" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df.iplot(kind='box')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3d Surface" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})\n", "df3.iplot(kind='surface',colorscale='rdylbu')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Spread" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df[['A','B']].iplot(kind='spread')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## histogram" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df['A'].iplot(kind='hist',bins=25)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df.iplot(kind='bubble',x='A',y='B',size='C')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## scatter_matrix()\n", "\n", "Similar to sns.pairplot()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df.scatter_matrix()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Great Job!" ] } ], "metadata": { "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }