Using Plotly in jupyter notebook

normal static plotting


In [1]:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib notebook

In [2]:
df = pd.read_csv('datasets/iris.csv',index_col=4,header=None,names=['SepalLength', 'SepalWidth', 'PetalLength','PetalWidth'],sep=',')
df.head()
x=df['SepalLength']
y=df['SepalWidth']
z=df['PetalLength']

In [3]:
df1 = df[df.index == 'Iris-setosa']
x1=df1['SepalLength']
y1=df1['SepalWidth']
z1=df1['PetalLength']
df2 = df[df.index == 'Iris-virginica']
x2=df2['SepalLength']
y2=df2['SepalWidth']
z2=df2['PetalLength']
df3 = df[df.index == 'Iris-versicolor']
x3=df3['SepalLength']
y3=df3['SepalWidth']
z3=df3['PetalLength']

In [4]:
#df[df.index == 'Iris-setosa']

In [7]:
#plt.scatter(df['SepalLength'],df['SepalWidth'],df['PetalLength'])
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(x1,y1,z1,c='b')
ax.scatter(x2,y2,z2,c='r')
ax.scatter(x3,y3,z3,c='g')


Out[7]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fbb1d8e5f60>

plotly of the same data set


In [7]:
import plotly.plotly as py
import plotly.graph_objs as go

In [8]:
np.unique(df.index)


Out[8]:
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)

In [9]:
df1 = df[df.index == 'Iris-setosa']
x1=df1['SepalLength']
y1=df1['SepalWidth']
z1=df1['PetalLength']
df2 = df[df.index == 'Iris-virginica']
x2=df2['SepalLength']
y2=df2['SepalWidth']
z2=df2['PetalLength']
df3 = df[df.index == 'Iris-versicolor']
x3=df3['SepalLength']
y3=df3['SepalWidth']
z3=df3['PetalLength']

In [10]:
trace1 = go.Scatter3d(
    name='setosa',
    x=x1,
    y=y1,
    z=z1,
    mode='markers',
    marker=dict(
        size=5,
        line=dict(
            #color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)
trace2 = go.Scatter3d(
    name='virginica',
    x=x2,
    y=y2,
    z=z2,
    mode='markers',
    marker=dict(
        size=5,
        line=dict(
            #color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)
trace3 = go.Scatter3d(
    name='versicolor',
    x=x3,
    y=y3,
    z=z3,
    mode='markers',
    marker=dict(
        size=5,
        line=dict(
            #color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)
data = [trace1,trace2,trace3]

layout = go.Layout(
    scene = dict(
                    xaxis = dict(
                        title='Sepal Length'),
                    yaxis = dict(
                        title='Sepal Width'),
                    zaxis = dict(
                        title='Petal Length'),
    ),
                    width=700,
                    margin=dict(
                    r=20, b=10,
                    l=10, t=10)
)
    
    
   




fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='simple-3d-scatter')


Out[10]:

In [ ]: