Skip to content

Instantly share code, notes, and snippets.

@santosjorge
Last active January 2, 2019 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save santosjorge/aba934a0d20023a136c2 to your computer and use it in GitHub Desktop.
Save santosjorge/aba934a0d20023a136c2 to your computer and use it in GitHub Desktop.
Plotly Pandas Like Visualization
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotly Visualization "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The aim of this notebook is to proivde guidelines on how to achieve parity with Pandas' visualization methods as explained in http://pandas.pydata.org/pandas-docs/stable/visualization.html with the use of **Plotly** and **Cufflinks**"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import cufflinks as cf\n",
"import numpy as np\n",
"from IPython.display import display,HTML"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%reload_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Theme"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cufflinks can set global theme (sytle) to used. \n",
"In this case we will use Matplotlib's `ggplot` style. "
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cf.set_config_file(theme='ggplot',sharing='public',offline=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Plotting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `iplot` method on Series and DataFrame is wrapper of Plotly's `plot` method"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3295.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Cufflinks can generate random data for different shapes\n",
"# Let's generate a single line with 1000 points\n",
"cf.datagen.lines(1,1000).iplot()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3297.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Generating 4 timeseries \n",
"df=cf.datagen.lines(4,1000)\n",
"df.iplot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can plot one column versus another using the *x* and *y* keywords in `iplot`"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3299.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()\n",
"df3['A'] = pd.Series(list(range(len(df3))))\n",
"df3.iplot(x='A', y='B')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bar Plots"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3301.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.ix[3].iplot(kind='bar',bargap=.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling a DataFrame’s `plot()` method with `kind='bar'` produces a multiple bar plot:"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3303.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df=pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])\n",
"df.iplot(kind='bar')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To produce a stacked bar plot, use `barmode=stack`"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3305.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='bar',barmode='stack')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get horizontal bar plots, pass `kind='barh'`"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3307.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='barh',barmode='stack',bargap=.1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Histograms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Historgrams can be used with `kind='histogram'`"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),\n",
" 'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3309.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='histogram')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Histogram can be stacked by using `barmode=stack`. Bin size can be changed by `bin` keyword. "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3311.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='histogram',barmode='stack',bins=20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Orientation can normalization can also be set for Histograms by using `orientation='horizontal'` and `histnorm=probability`."
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3313.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='histogram',columns=['a'],orientation='h',histnorm='probability')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Histograms (and any other kind of plot) can be set in a multiple layout by using `subplots=True`"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3315.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_h=cf.datagen.histogram(4)\n",
"df_h.iplot(kind='histogram',subplots=True,bins=50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Box Plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boxplots can be drawn calling a `Series` and `DataFrame` with `kind='box'`"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3317.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 65,
"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')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Grouping values"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] )\n",
"df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Grouping values by generating a list of figures"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"figs=[df[df['X']==d][['Col1','Col2']].iplot(kind='box',asFigure=True) for d in pd.unique(df['X']) ]"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3319.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cf.iplot(cf.subplots(figs))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Grouping values and ammending the keys"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def by(df,category):\n",
" l=[]\n",
" for cat in pd.unique(df[category]):\n",
" _df=df[df[category]==cat]\n",
" del _df[category]\n",
" _df=_df.rename(columns=dict([(k,'{0}_{1}'.format(cat,k)) for k in _df.columns]))\n",
" l.append(_df.iplot(kind='box',asFigure=True))\n",
" return l\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3321.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cf.iplot(cf.subplots(by(df,'X')))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Area Plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can create area plots with Series.plot and DataFrame.plot by passing `kind='area'`. 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.\n",
"\n",
"To fill the area you can use `fill=True`"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3323.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='area',fill=True,opacity=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For non-stacked charts you can use `kind=scatter` with `fill=True`. Alpha value is set to 0.3 unless otherwise specified:"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3325.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(fill=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scatter Plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can create scatter plots with DataFrame.plot by passing `kind='scatter'`. Scatter plot requires numeric columns for x and y axis. These can be specified by x and y keywords each, otherwise the DataFrame index will be used as `x`"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3327.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='scatter',x='a',y='b',mode='markers')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Colors can be assigned as either a list or dicitonary by using `color`. \n",
"The marker symbol can be defined by using `symbol`"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3329.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='scatter',mode='markers',symbol='dot',colors=['orange','teal','blue','yellow'],size=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bubble charts can be used with `kind=bubble` and by assigning one column as the `size`"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3331.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(kind='bubble',x='a',y='b',size='c')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scatter Matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can create a scatter plot matrix using the function `scatter_matrix` "
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3333.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.scatter_matrix()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subplots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subplots can be defined with `subplots=True`. The shape of the output can also be determined with `shape=(rows,cols)`. If omitted then the subplot shape will automatically defined. \n",
"\n",
"Axes can be shared across plots with `shared_xaxes=True` as well as `shared_yaxes=True`"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df=cf.datagen.lines(4)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3335.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(subplots=True,shape=(4,1),shared_xaxes=True,vertical_spacing=.02,fill=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subplot Title can be set with `subplot_titles`. If set to `True` then the column names will be used. Otherwise a list of strings can be passed. "
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3337.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(subplots=True,subplot_titles=True,legend=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Irregular Subplots can also be drawn using `specs`. \n",
"For example, for getting a charts that spans across 2 rows we can use `specs=[[{'rowspan':2},{}],[None,{}]]`. \n",
"For a full set of advanced layout you can see `help(cufflinks.subplots)`"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df=cf.datagen.bubble(10,50,mode='stocks')"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"figs=cf.figures(df,[dict(kind='histogram',keys='x',color='blue'),\n",
" dict(kind='scatter',mode='markers',x='x',y='y',size=5),\n",
" dict(kind='scatter',mode='markers',x='x',y='y',size=5,color='teal')],asList=True)\n",
"figs.append(cf.datagen.lines(1).figure(bestfit=True,colors=['blue'],bestfit_colors=['pink']))\n",
"base_layout=cf.tools.get_base_layout(figs)\n",
"sp=cf.subplots(figs,shape=(3,2),base_layout=base_layout,vertical_spacing=.15,horizontal_spacing=.03,\n",
" specs=[[{'rowspan':2},{}],[None,{}],[{'colspan':2},None]],\n",
" subplot_titles=['Histogram','Scatter 1','Scatter 2','Bestfit Line'])\n",
"sp['layout'].update(showlegend=False)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3339.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cf.iplot(sp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Shapes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lines can be added with `hline` and `vline` for horizontal and vertical lines respectively. \n",
"These can be either a list of values (relative to the axis) or a dictionary. "
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df=cf.datagen.lines(3,columns=['a','b','c'])"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3341.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(hline=[2,4],vline=['2015-02-10'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"More advanced parameters can be passed in the form of a dictionary, including `width` and `color` and `dash` for the line dash type. "
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3343.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(hline=[dict(y=-1,color='blue',width=3),dict(y=1,color='pink',dash='dash')])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Shaded areas can be plotted using `hspan` and `vspan` for horizontal and vertical areas respectively. \n",
"These can be set with a list of paired tuples (v0,v1) or a list of dictionaries with further parameters. "
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3345.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(hspan=[(-1,1),(2,5)])"
]
},
{
"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": 90,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3347.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iplot(vspan={'x0':'2015-02-15','x1':'2015-03-15','color':'teal','fill':True,'opacity':.4})"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3349.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Plotting resistance lines\n",
"max_vals=df.max().values.tolist()\n",
"resistance=[dict(kind='line',y=i,color=j,width=2) for i,j in zip(max_vals,['red','blue','pink'])]\n",
"df.iplot(hline=resistance)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Different shapes can also be used with `shapes` and identifying the `kind` which can be either *line*, *rect* or *circle*"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Get min to max values\n",
"\n",
"df_a=df['a']\n",
"max_val=df_a.max()\n",
"min_val=df_a.min()\n",
"max_date=df_a[df_a==max_val].index[0].strftime('%Y-%m-%d')\n",
"min_date=df_a[df_a==min_val].index[0].strftime('%Y-%m-%d')\n",
"shape1=dict(kind='line',x0=max_date,y0=max_val,x1=min_date,y1=min_val,color='blue',width=2)\n",
"shape2=dict(kind='rect',x0=max_date,x1=min_date,fill=True,color='gray',opacity=.3)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3351.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_a.iplot(shapes=[shape1,shape2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Other Shapes"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x0 = np.random.normal(2, 0.45, 300)\n",
"y0 = np.random.normal(2, 0.45, 300)\n",
"\n",
"x1 = np.random.normal(6, 0.4, 200)\n",
"y1 = np.random.normal(6, 0.4, 200)\n",
"\n",
"x2 = np.random.normal(4, 0.3, 200)\n",
"y2 = np.random.normal(4, 0.3, 200)\n",
"\n",
"distributions = [(x0,y0),(x1,y1),(x2,y2)]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dfs=[pd.DataFrame(dict(x=i,y=j)) for i,j in distributions]"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d=cf.Data()\n",
"gen=cf.colorgen(scale='ggplot')\n",
"for df in dfs:\n",
" d_=df.figure(kind='scatter',mode='markers',x='x',y='y',size=5,colors=gen.next())['data']\n",
" for _ in d_:\n",
" d.append(_)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gen=cf.colorgen(scale='ggplot')\n",
"shapes=[cf.tools.get_shape(kind='circle',x0=min(x),x1=max(x),\n",
" y0=min(y),y1=max(y),color=gen.next(),fill=True,\n",
" opacity=.3,width=.4) for x,y in distributions]"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~jorgesantos/3353.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig=cf.Figure(data=d)\n",
"fig['layout']=cf.getLayout(shapes=shapes,legend=False,title='Distribution Comparison')\n",
"cf.iplot(fig,validate=False)"
]
},
{
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment