{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Cufflinks binds [Plotly](https://plotly.com/python) directly to [pandas](http://pandas.pydata.org/) dataframes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.tools as tls\n", "tls.embed('https://plotly.com/~cufflinks/8')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Packages\n", "Run `! pip install cufflinks --upgrade` to install Cufflinks. In addition to [Plotly](https://plotly.com/python), [pandas](http://pandas.pydata.org/) and Cufflinks, this tutorial will also use [NumPy](http://www.numpy.org/). " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8.2\n" ] } ], "source": [ "import plotly.plotly as py\n", "import cufflinks as cf\n", "import pandas as pd\n", "import numpy as np\n", "print cf.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dataframes\n", "With Plotly's Python library, you can describe figures with DataFrame's series and index's" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = cf.datagen.lines()\n", "\n", "py.iplot([{\n", " 'x': df.index,\n", " 'y': df[col],\n", " 'name': col\n", "} for col in df.columns], filename='cufflinks/simple-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But with `cufflinks`, you can plot them directly" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='scatter', filename='cufflinks/cf-simple-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Almost every chart that you make in `cufflinks` will be created with just one line of code." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])\n", "df.scatter_matrix(filename='cufflinks/scatter-matrix', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Charts created with `cufflinks` are synced with your online Plotly account. You'll need to [configure your credentials](https://plotly.com/python/getting-started/) to get started. `cufflinks` can also be configured to work offline in IPython notebooks with [Plotly Offline](https://plotly.com/python/offline). To get started with Plotly Offline, [download a trial library](http://purchasing.plot.ly/plotly-offline-ipython) and run `cf.go_offline()`." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cf.go_online() # switch back to online mode, where graphs are saved on your online plotly account" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, plotly graphs are *public*. Make them private by setting `world_readable` to `False`" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.a.iplot(kind='histogram', world_readable=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only *you* (the creator) will be able to see this chart, or change the global, default settings with `cf.set_config_file`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cf.set_config_file(offline=False, world_readable=True, theme='ggplot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chart Types\n", "##### Line Charts" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(np.random.randn(1000, 2), columns=['A', 'B']).cumsum()\n", "df.iplot(filename='cufflinks/line-example')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot one column vs another with `x` and `y` keywords" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(x='A', y='B', filename='cufflinks/x-vs-y-line-example')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Bar Charts\n", "Download some civic data. A time series log of the 311 complaints in NYC." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/plotly/widgets/master/ipython-examples/311_150k.csv', parse_dates=True, index_col=1)\n", "df.head(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "series = df['Complaint Type'].value_counts()[:20]\n", "series.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot a `series` directly" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "series.iplot(kind='bar', yTitle='Number of Complaints', title='NYC 311 Complaints',\n", " filename='cufflinks/categorical-bar-chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot a dataframe row as a bar" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(np.random.rand(10, 4), columns=['A', 'B', 'C', 'D'])\n", "row = df.ix[5]\n", "row.iplot(kind='bar', filename='cufflinks/bar-chart-row')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call `iplot(kind='bar')` on a dataframe to produce a grouped bar chart" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='bar', filename='cufflinks/grouped-bar-chart')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='bar', barmode='stack', filename='cufflinks/grouped-bar-chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember: plotly charts are interactive. Click on the legend entries to hide-and-show traces, click-and-drag to zoom, double-click to autoscale, shift-click to drag.\n", "\n", "![Click on legend entries to hide and show traces](http://i.imgur.com/419Bmdy.gif)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make your bar charts horizontal with `kind='barh'`" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='barh',barmode='stack', bargap=.1, filename='cufflinks/barh')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Themes\n", "\n", "cufflinks ships with a few themes. View available themes with `cf.getThemes`, apply them with `cf.set_config_file`" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['pearl', 'white', 'ggplot', 'solar', 'space']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cf.getThemes()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cf.set_config_file(theme='pearl')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Histograms" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({'a': np.random.randn(1000) + 1,\n", " 'b': np.random.randn(1000),\n", " 'c': np.random.randn(1000) - 1})\n", "\n", "df.iplot(kind='histogram', filename='cufflinks/basic-histogram')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Customize your histogram with \n", "- `barmode` (`overlay` | `group` | `stack`)\n", "- `bins` (`int`)\n", "- `histnorm` (`'' | 'percent' | 'probability' | 'density' | 'probability density'`)\n", "- `histfunc` (`'count' | 'sum' | 'avg' | 'min' | 'max'`)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='histogram', barmode='stack', bins=100, histnorm='probability', filename='cufflinks/customized-histogram')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like every chart type, split your traces into subplots or small-multiples with `subplots` and optionally `shape`. More on `subplots` below." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='histogram', subplots=True, shape=(3, 1), filename='cufflinks/histogram-subplots')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Box Plots" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])\n", "df.iplot(kind='box', filename='cufflinks/box-plots')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Area Charts\n", "\n", "To produce stacked area plot, each column must be either all positive or all negative values.\n", "\n", "When input data contains `NaN`, it will be automatically filled by 0. If you want to drop or fill by different values, use `dataframe.dropna()` or `dataframe.fillna()` before calling plot." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='area', fill=True, filename='cuflinks/stacked-area')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For non-stacked area charts, set `kind=scatter` with `fill=True`" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(fill=True, filename='cuflinks/filled-area')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Scatter Plot\n", "\n", "Set `x` and `y` as column names. If `x` isn't supplied, `df.index` will be used." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt', sep='\\t')\n", "df2007 = df[df.year==2007]\n", "df1952 = df[df.year==1952]\n", "\n", "df2007.iplot(kind='scatter', mode='markers', x='gdpPercap', y='lifeExp', filename='cufflinks/simple-scatter')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting multiple column scatter plots isn't as easy with cufflinks. Here is an example with Plotly's native syntax" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = {\n", " 'data': [\n", " {'x': df2007.gdpPercap, 'y': df2007.lifeExp, 'text': df2007.country, 'mode': 'markers', 'name': '2007'},\n", " {'x': df1952.gdpPercap, 'y': df1952.lifeExp, 'text': df1952.country, 'mode': 'markers', 'name': '1952'}\n", " ],\n", " 'layout': {\n", " 'xaxis': {'title': 'GDP per Capita', 'type': 'log'},\n", " 'yaxis': {'title': \"Life Expectancy\"}\n", " }\n", "}\n", "py.iplot(fig, filename='cufflinks/multiple-scatter')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Grouping isn't as easy either. But, with Plotly's native syntax:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.iplot(\n", " {\n", " 'data': [\n", " {\n", " 'x': df[df['year']==year]['gdpPercap'],\n", " 'y': df[df['year']==year]['lifeExp'],\n", " 'name': year, 'mode': 'markers',\n", " } for year in [1952, 1982, 2007]\n", " ],\n", " 'layout': {\n", " 'xaxis': {'title': 'GDP per Capita', 'type': 'log'},\n", " 'yaxis': {'title': \"Life Expectancy\"}\n", " }\n", "}, filename='cufflinks/scatter-group-by')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bubble Charts\n", "Add `size` to create a bubble chart. Add hover text with the `text` attribute." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2007.iplot(kind='bubble', x='gdpPercap', y='lifeExp', size='pop', text='country',\n", " xTitle='GDP per Capita', yTitle='Life Expectancy',\n", " filename='cufflinks/simple-bubble-chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Subplots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`subplots=True` partitions columns into separate subplots. Specify rows and columns with `shape=(rows, cols)` and share axes with `shared_xaxes=True` and `shared_yaxes=True`." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df=cf.datagen.lines(4)\n", "df.iplot(subplots=True, shape=(4,1), shared_xaxes=True, fill=True, filename='cufflinks/simple-subplots')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add subplot titles with `subplot_titles` as a list of titles or `True` to use column names." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(subplots=True, subplot_titles=True, legend=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Scatter matrix" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.scatter_matrix(filename='cufflinks/scatter-matrix-subplot', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Heatmaps" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cf.datagen.heatmap(20,20).iplot(kind='heatmap',colorscale='spectral',\n", " filename='cufflinks/simple-heatmap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Lines and Shaded Areas\n", "\n", "Use `hline` and `vline` for horizontal and vertical lines." ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df=cf.datagen.lines(3,columns=['a','b','c'])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(hline=[2,4],vline=['2015-02-10'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw shaded regions with `hspan`" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(hspan=[(-1,1),(2,5)], filename='cufflinks/shaded-regions')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extra parameters can be passed in the form of dictionaries, width, fill, color, fillcolor, opacity" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(vspan={'x0':'2015-02-15','x1':'2015-03-15','color':'rgba(30,30,30,0.3)','fill':True,'opacity':.4}, \n", " filename='cufflinks/custom-regions')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing Figures\n", "\n", "`cufflinks` is designed for simple one-line charting with Pandas and Plotly. All of the Plotly chart attributes are not directly assignable in the `df.iplot` call signature.\n", "\n", "To update attributes of a `cufflinks` chart that aren't available, first convert it to a figure (`asFigure=True`), then tweak it, then plot it with `plotly.plotly.iplot`.\n", "\n", "Here is an example of a simple plotly figure. You can find more examples in [our online python documentation](https://plotly.com/python)." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from plotly.graph_objs import *\n", "py.iplot({\n", " 'data': [\n", " Bar(**{\n", " 'x': [1, 2, 3],\n", " 'y': [3, 1, 5],\n", " 'name': 'first trace',\n", " 'type': 'bar'\n", " }),\n", " Bar(**{\n", " 'x': [1, 2, 3],\n", " 'y': [4, 3, 6],\n", " 'name': 'second trace',\n", " 'type': 'bar'\n", " })\n", " ],\n", " 'layout': Layout(**{\n", " 'title': 'simple example'\n", " })\n", "}, filename='cufflinks/simple-plotly-example')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`cufflinks` generates these figure's that describe plotly graphs. For example, this graph:" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iplot(kind='scatter', filename='cufflinks/simple-scatter-example')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "has this description:" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Figure(\n", " data=Data([\n", " Scatter(\n", " x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..' ],\n", " y=array([ 5.35393544e-01, -3.51020567e-01, -1.34207933e+00,\n", " ..,\n", " mode='lines',\n", " name='a',\n", " line=Line(\n", " color='rgba(255, 153, 51, 1.0)',\n", " width='1.3'\n", " )\n", " ),\n", " Scatter(\n", " x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..' ],\n", " y=array([ -2.58404773, -1.91629648, -1.88997988, -1.09846618,..,\n", " mode='lines',\n", " name='b',\n", " line=Line(\n", " color='rgba(55, 128, 191, 1.0)',\n", " width='1.3'\n", " )\n", " ),\n", " Scatter(\n", " x=['2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '..' ],\n", " y=array([ 0.46611148, 1.06107695, 1.06206594, -0.56030965, -0...,\n", " mode='lines',\n", " name='c',\n", " line=Line(\n", " color='rgba(50, 171, 96, 1.0)',\n", " width='1.3'\n", " )\n", " )\n", " ]),\n", " layout=Layout(\n", " legend=Legend(\n", " font=Font(\n", " color='#4D5663'\n", " ),\n", " bgcolor='#F5F6F9'\n", " ),\n", " paper_bgcolor='#F5F6F9',\n", " plot_bgcolor='#F5F6F9',\n", " xaxis1=XAxis(\n", " title='',\n", " titlefont=Font(\n", " color='#4D5663'\n", " ),\n", " tickfont=Font(\n", " color='#4D5663'\n", " ),\n", " gridcolor='#E1E5ED',\n", " zerolinecolor='#E1E5ED'\n", " ),\n", " yaxis1=YAxis(\n", " title='',\n", " titlefont=Font(\n", " color='#4D5663'\n", " ),\n", " zeroline=False,\n", " tickfont=Font(\n", " color='#4D5663'\n", " ),\n", " gridcolor='#E1E5ED',\n", " zerolinecolor='#E1E5ED'\n", " )\n", " )\n", ")\n" ] } ], "source": [ "figure = df.iplot(kind='scatter', asFigure=True)\n", "print figure.to_string()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, if you want to edit any attribute of a Plotly graph from cufflinks, first convert it to a figure and then edit the figure objects. Let's add a yaxis title, tick suffixes, and new legend names to this example:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "figure['layout']['yaxis1'].update({'title': 'Price', 'tickprefix': '$'})\n", "for i, trace in enumerate(figure['data']):\n", " trace['name'] = 'Trace {}'.format(i)\n", " \n", "py.iplot(figure, filename='cufflinks/customized-chart')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[See more examples of Plotly graphs](https://plotly.com/python/) or [view the entire reference of valid attributes](https://plotly.com/python/reference/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Cufflinks Reference\n", "Cufflinks is [open source on github](https://github.com/santosjorge/cufflinks)!" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method _iplot in module cufflinks.plotlytools:\n", "\n", "_iplot(self, data=None, layout=None, filename='', world_readable=None, kind='scatter', title='', xTitle='', yTitle='', zTitle='', theme=None, colors=None, colorscale=None, fill=False, width=None, mode='lines', symbol='dot', size=12, barmode='', sortbars=False, bargap=None, bargroupgap=None, bins=None, histnorm='', histfunc='count', orientation='v', boxpoints=False, annotations=None, keys=False, bestfit=False, bestfit_colors=None, categories='', x='', y='', z='', text='', gridcolor=None, zerolinecolor=None, margin=None, subplots=False, shape=None, asFrame=False, asDates=False, asFigure=False, asImage=False, dimensions=(1116, 587), asPlot=False, asUrl=False, online=None, **kwargs) method of pandas.core.frame.DataFrame instance\n", " Returns a plotly chart either as inline chart, image of Figure object\n", " \n", " Parameters:\n", " -----------\n", " data : Data\n", " Plotly Data Object.\n", " If not entered then the Data object will be automatically\n", " generated from the DataFrame.\n", " data : Data\n", " Plotly Data Object.\n", " If not entered then the Data object will be automatically\n", " generated from the DataFrame.\n", " layout : Layout\n", " Plotly layout Object\n", " If not entered then the Layout objet will be automatically\n", " generated from the DataFrame.\n", " filename : string\n", " Filename to be saved as in plotly account\n", " world_readable : bool\n", " If False then it will be saved as a private file\n", " kind : string\n", " Kind of chart\n", " scatter\n", " bar\n", " box\n", " spread\n", " ratio\n", " heatmap\n", " surface\n", " histogram\n", " bubble\n", " bubble3d\n", " scatter3d\n", " title : string\n", " Chart Title\n", " xTitle : string\n", " X Axis Title\n", " yTitle : string\n", " Y Axis Title\n", " zTitle : string\n", " zTitle : string\n", " Z Axis Title\n", " Applicable only for 3d charts\n", " theme : string\n", " Layout Theme\n", " solar\n", " pearl\n", " white\n", " see cufflinks.getThemes() for all\n", " available themes\n", " colors : list or dict\n", " {key:color} to specify the color for each column\n", " [colors] to use the colors in the defined order\n", " colorscale : str\n", " Color scale name\n", " If the color name is preceded by a minus (-)\n", " then the scale is inversed\n", " Only valid if 'colors' is null\n", " See cufflinks.colors.scales() for available scales\n", " fill : bool\n", " Filled Traces\n", " width : int\n", " Line width\n", " mode : string\n", " Plotting mode for scatter trace\n", " lines\n", " markers\n", " lines+markers\n", " lines+text\n", " markers+text\n", " lines+markers+text\n", " symbol : string\n", " The symbol that is drawn on the plot for each marker\n", " Valid only when mode includes markers\n", " dot\n", " cross\n", " diamond\n", " square\n", " triangle-down\n", " triangle-left\n", " triangle-right\n", " triangle-up\n", " x\n", " size : string or int\n", " Size of marker\n", " Valid only if marker in mode\n", " barmode : string\n", " Mode when displaying bars\n", " group\n", " stack\n", " overlay\n", " * Only valid when kind='bar'\n", " sortbars : bool\n", " Sort bars in descending order\n", " * Only valid when kind='bar'\n", " bargap : float\n", " Sets the gap between bars\n", " [0,1)\n", " * Only valid when kind is 'histogram' or 'bar'\n", " bargroupgap : float\n", " Set the gap between groups\n", " [0,1)\n", " * Only valid when kind is 'histogram' or 'bar'\n", " bins : int\n", " Specifies the number of bins\n", " * Only valid when kind='histogram'\n", " histnorm : string\n", " '' (frequency)\n", " percent\n", " probability\n", " density\n", " probability density\n", " Sets the type of normalization for an histogram trace. By default\n", " the height of each bar displays the frequency of occurrence, i.e.,\n", " the number of times this value was found in the\n", " corresponding bin. If set to 'percent', the height of each bar\n", " displays the percentage of total occurrences found within the\n", " corresponding bin. If set to 'probability', the height of each bar\n", " displays the probability that an event will fall into the\n", " corresponding bin. If set to 'density', the height of each bar is\n", " equal to the number of occurrences in a bin divided by the size of\n", " the bin interval such that summing the area of all bins will yield\n", " the total number of occurrences. If set to 'probability density',\n", " the height of each bar is equal to the number of probability that an\n", " event will fall into the corresponding bin divided by the size of\n", " the bin interval such that summing the area of all bins will yield\n", " 1.\n", " * Only valid when kind='histogram'\n", " histfunc : string\n", " count\n", " sum\n", " avg\n", " min\n", " max\n", " Sets the binning function used for an histogram trace.\n", " * Only valid when kind='histogram'\n", " orientation : string\n", " h\n", " v\n", " Sets the orientation of the bars. If set to 'v', the length of each\n", " | bar will run vertically. If set to 'h', the length of each bar will\n", " | run horizontally\n", " * Only valid when kind is 'histogram','bar' or 'box'\n", " boxpoints : string\n", " Displays data points in a box plot\n", " outliers\n", " all\n", " suspectedoutliers\n", " False\n", " annotations : dictionary\n", " Dictionary of annotations\n", " {x_point : text}\n", " keys : list of columns\n", " List of columns to chart.\n", " Also can be usded for custom sorting.\n", " bestfit : boolean or list\n", " If True then a best fit line will be generated for\n", " all columns.\n", " If list then a best fit line will be generated for\n", " each key on the list.\n", " bestfit_colors : list or dict\n", " {key:color} to specify the color for each column\n", " [colors] to use the colors in the defined order\n", " categories : string\n", " Name of the column that contains the categories\n", " x : string\n", " Name of the column that contains the x axis values\n", " y : string\n", " Name of the column that contains the y axis values\n", " z : string\n", " Name of the column that contains the z axis values\n", " text : string\n", " Name of the column that contains the text values\n", " gridcolor : string\n", " Grid color\n", " zerolinecolor : string\n", " Zero line color\n", " margin : dict or tuple\n", " Dictionary (l,r,b,t) or\n", " Tuple containing the left,\n", " right, bottom and top margins\n", " subplots : bool\n", " If true then each trace is placed in\n", " subplot layout\n", " shape : (rows,cols)\n", " Tuple indicating the size of rows and columns\n", " If omitted then the layout is automatically set\n", " * Only valid when subplots=True\n", " asFrame : bool\n", " If true then the data component of Figure will\n", " be of Pandas form (Series) otherwise they will\n", " be index values\n", " asDates : bool\n", " If true it truncates times from a DatetimeIndex\n", " asFigure : bool\n", " If True returns plotly Figure\n", " asImage : bool\n", " If True it returns Image\n", " * Only valid when asImage=True\n", " dimensions : tuple(int,int)\n", " Dimensions for image\n", " (width,height)\n", " asPlot : bool\n", " If True the chart opens in browser\n", " asUrl : bool\n", " If True the chart url is returned. No chart is displayed.\n", " online : bool\n", " If True then the chart is rendered on the server\n", " even when running in offline mode.\n", "\n" ] } ], "source": [ "help(df.iplot)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "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": [ "#!pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'cufflinks.ipynb', 'ipython-notebooks/cufflinks/', 'Cufflinks - Easy Pandas DataFrame Graphing with Plotly | plotly',\n", " 'An overview of cufflinks, a library for easy interactive Pandas charting with Plotly.',\n", " title = 'Cufflinks - Easy Pandas DataFrame Graphing with Plotly | plotly',\n", " name = 'Cufflinks',\n", " thumbnail='thumbnail/line-plot.jpg', language='python',\n", " ipynb= '~notebook_demo/3')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": 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.11" } }, "nbformat": 4, "nbformat_minor": 1 }