Plotly Tutorial


When I initially started using Plotly, I had problems getting my visualizations to show up on my blog. I'm making this post to demonstrate how to use plotly visualizations in Jupyter Notebooks as well as how to embed them into websites. I also have a few cool tips for Jupyter Notebook.

Import Libraries

In [1]:
import pandas as pd
import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()
In [2]:
#example dataset
df1 = pd.DataFrame(np.random.randn(200,5),columns='A B C D E'.split())
In [3]:
df1.head()
Out[3]:
A B C D E
0 -1.576554 -0.277349 -0.129550 -0.480986 0.467955
1 0.082454 0.328599 1.085550 0.201724 -0.339648
2 -0.110213 0.662517 -0.636204 0.527291 1.057361
3 -1.198748 -0.077928 0.200904 -0.351167 1.663235
4 -0.250557 -1.866524 -0.920728 -2.063552 1.156649

The code below normally allows the user to display Plotly visualizations in Jupyter Notebook, but the issue is that this does not display on a static web page. This is problematic because people usually like to see visualizations and not a blank box.

In [4]:
#scatter plot
df1.iplot(kind='scatter',x='A',y='B',mode='markers',size=10, title='Scatter Plot')

I fix this problem by exporting my plot to plot.ly (you will see a button in the bottom right corner of your graph) and embedding the plot as shown below:

In [5]:
import plotly.tools as tls
tls.embed('https://plot.ly/~ruer98/3/') #embeds plot in notebook
Out[5]:
In [6]:
#boxplot

#df1.iplot(kind='box')
tls.embed('https://plot.ly/~ruer98/5/')
Out[6]:
In [7]:
#spread plot

#df1[['A','B']].iplot(kind='spread')
tls.embed('https://plot.ly/~ruer98/7/')
Out[7]:
In [8]:
#histogram

#df1['C'].iplot(kind='hist',bins=50)
tls.embed('https://plot.ly/~ruer98/9/')
Out[8]:
In [9]:
#bubble plot

#df1.iplot(kind='bubble',x='C',y='D',size='E')
tls.embed('https://plot.ly/~ruer98/11/')
Out[9]:
In [10]:
#scatter matrix
#this is basically an interactive version of sns.pairplot()

#df1.scatter_matrix()
tls.embed('https://plot.ly/~ruer98/13/')
Out[10]:
In [11]:
#more example data
df2 = pd.DataFrame({'Category':'A B C'.split(),'Values':[46,37,52]})
df2.head()
Out[11]:
Category Values
0 A 46
1 B 37
2 C 52
In [12]:
#bar plot

#df2.iplot(kind='bar',x='Category',y='Values',xTitle='Category',yTitle='Values',title='Bar Plot')
tls.embed('https://plot.ly/~ruer98/15/')
Out[12]:
In [13]:
#even more example data
df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})
df3.head()
Out[13]:
x y z
0 1 10 5
1 2 20 4
2 3 30 3
3 4 20 2
4 5 10 1
In [14]:
#surface plot

#df3.iplot(kind='surface', colorscale='rdylgn')
tls.embed('https://plot.ly/~ruer98/17/')
Out[14]:

That was a brief overview of the powerful plotting that Plotly is capable of. Here is a few cool things you can do with...

Ipython.display


display math formulas in your notebook using Latex:

In [15]:
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$

embed HTML and IFrame content:

In [16]:
from IPython.display import IFrame, HTML
IFrame(src='https://s3.amazonaws.com/duhaime/blog/visualizations/isolation-forests.html', width=700, height=600)
Out[16]:

or even embed a Youtube video:

In [17]:
from IPython.display import YouTubeVideo
YouTubeVideo("wupToqz1e2g")
Out[17]:

I hope this post was enjoyable and informative. As always I encourage any comments, questions, and suggestions that you may have.