plotly example
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]:
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)
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.