plotly example

  |   Source

Plotly is one of my favourite tools for data visualization. When I started writing this blog, plotly graphs were not embedded in the correct format. This is a simple post on how to embed your plotly graphs on a blog/webpage using a Jupyter notebook.

In [1]:
import pandas as pd
from sklearn import datasets

import plotly
import plotly.plotly as py
plotly.tools.set_credentials_file(username='', api_key='')
from plotly.graph_objs import *

import plotly.figure_factory as FF
In [2]:
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns=["Sepal_Length","Sepal_Width", "Petal_Length", "Petal_Width"])

class_dict = {0:'Setosa', 1:'Versicolour', 2:'Virginica'}
iris_df['Iris_Class'] = iris.target
iris_df['Iris_Class'] = iris_df['Iris_Class'].map(class_dict)
In [3]:
iris_df.head()
Out[3]:
Sepal_Length Sepal_Width Petal_Length Petal_Width Iris_Class
0 5.1 3.5 1.4 0.2 Setosa
1 4.9 3.0 1.4 0.2 Setosa
2 4.7 3.2 1.3 0.2 Setosa
3 4.6 3.1 1.5 0.2 Setosa
4 5.0 3.6 1.4 0.2 Setosa

Scatter plot matrix

In [4]:
fig = FF.create_scatterplotmatrix(iris_df, index='Iris_Class',
                                  height=800, width=800, diag='box',
                                 colormap= dict(
                                      Setosa = '#00F5FF',
                                      Versicolour = '#32CD32',
                                      Virginica = '#DAA520'
                                  ),
                                  colormap_type='cat')
plot_url = py.plot(fig, filename = 'Scatter Plot matrix - Iris Dataset', auto_open=False)
print (plot_url)
https://plot.ly/~raunakm90/59

To embed plotly graphs, we need to generate HTML code for the graph. Following piece of code allows us to do it very easily.

In [5]:
import plotly.tools as tls
from IPython.display import HTML

embed_output = tls.get_embed(plot_url, height=900)
HTML(embed_output)
Out[5]:

We need to now inlcude this in our html page.

Follow @raunakmundada Follow @raunakm90