Installing Necessary Libraries¶
In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
from plotly.offline import iplot
import plotly as py
import plotly.tools as tls
import chart_studio.plotly as cpy
import chart_studio.tools as ctls
In [3]:
import cufflinks as cf
Using cufflinks and iplot() to create different plots¶
- line
- scatter
- bar
- box
- spread
- histogram
- bubble
- surface
- heatmap
Knowing the version of the packages¶
In [1]:
print(py.__version__)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-2221253c3501> in <module> ----> 1 print(py.__version__) NameError: name 'py' is not defined
In [10]:
print(cf.__version__)
0.17.3
Embedding Online Plots to the Jupyter Notebook¶
In [4]:
ctls.embed('https://plotly.com/~cufflinks/8')
Out[4]:
Using Plotly-Cufflinks offline¶
In [4]:
py.offline.init_notebook_mode(connected=True)
In [5]:
cf.go_offline()
Connecting Jupyter Notebook with Plotly Account¶
In [11]:
import chart_studio
chart_studio.tools.set_credentials_file(username='PriyankaJha', api_key='EgKBYXoVIi8Aw32QlKrf')
lineplot¶
In [6]:
#generating some pseudo data for analysis
df = pd.DataFrame(np.random.randn(100, 3), columns=['A', 'B', 'C'])
df['A'] = df['A'].cumsum()
df['B'] = df['B'].cumsum()
df['C'] = df['C'].cumsum()
df.head()
Out[6]:
| A | B | C | |
|---|---|---|---|
| 0 | -0.405230 | 0.070950 | -2.137773 |
| 1 | 0.111522 | 1.920144 | -2.159755 |
| 2 | -0.626906 | 3.780232 | -2.015929 |
| 3 | 0.716704 | 3.244089 | -1.326465 |
| 4 | 0.211570 | 2.747247 | -1.485680 |
In [7]:
#different themes for plots in plotly
cf.getThemes()
Out[7]:
['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']
In [12]:
df.iplot(theme='solar')
scatterplot¶
In [13]:
df.iplot(x='A', y='B', mode='markers', symbol='diamond', size=9, theme='solar')
#use box or lasso select to highlight the desired markers
barplot¶
In [10]:
titanic = sns.load_dataset('titanic')
titanic.head()
Out[10]:
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
In [12]:
titanic.iplot(kind='bar',
x='sex',
y='survived',
theme='solar',
color='lightgreen',
xTitle='Sex',
yTitle='Number of Survived Individuals',
title='Survival on the Basis of Sex')
stackedplot¶
In [14]:
df.iplot(kind='bar', bargap=0.4, barmode='stack', theme='solar')
In [14]:
#horizontal bar graph
df.iplot(kind='barh', bargap=0.4, theme='solar')
boxplot¶
In [15]:
df.iplot(kind='box', theme='solar')
areaplot¶
In [16]:
#generating another set of dataset with only positive values
df2 = pd.DataFrame(np.random.randn(150, 3), columns=['A', 'B', 'C'])
df2['A'] = df2['A'].cumsum() + 15
df2['B'] = df2['B'].cumsum() + 15
df2['C'] = df2['C'].cumsum() + 15
df2.head()
Out[16]:
| A | B | C | |
|---|---|---|---|
| 0 | 15.161858 | 15.444999 | 15.744817 |
| 1 | 16.349772 | 15.269878 | 16.264184 |
| 2 | 15.119788 | 13.444587 | 15.544242 |
| 3 | 15.642109 | 11.379189 | 14.308738 |
| 4 | 16.190989 | 12.725358 | 13.952934 |
In [18]:
df2.iplot(kind='area', fill=True, theme='solar')
surfaceplot¶
In [19]:
df3 = pd.DataFrame({
'X': [10, 20, 30, 20, 10],
'Y': [10, 20, 30, 20, 10],
'Z': [10, 20, 30, 20, 10]
})
df3
Out[19]:
| X | Y | Z | |
|---|---|---|---|
| 0 | 10 | 10 | 10 |
| 1 | 20 | 20 | 20 |
| 2 | 30 | 30 | 30 |
| 3 | 20 | 20 | 20 |
| 4 | 10 | 10 | 10 |
In [19]:
#color scales for surface and heatmaps
cf.colors.scales()
accent
blues
brbg
bugn
bupu
dark2
dflt
ggplot
gnbu
greens
greys
henanigans
oranges
original
orrd
paired
pastel1
pastel2
piyg
plotly
polar
prgn
pubu
pubugn
puor
purd
purples
rdbu
rdgy
rdpu
rdylbu
rdylgn
reds
set1
set2
set3
spectral
ylgn
ylgnbu
ylorbr
ylorrd
In [20]:
df3.iplot(kind='surface', colorscale='spectral')
In [140]:
# help(cf.datagen)
In [21]:
cf.datagen.sinwave(12, 0.15).iplot(kind='surface', colorscale='set1')
3Dsacatterplot¶
In [23]:
cf.datagen.scatter3d(2, 150, mode='stocks').iplot(kind='scatter3d',
x='x',
y='y',
z='z')
spreadplot¶
In [24]:
df[['A', 'B']].iplot(kind='spread', theme='solar')
C:\Users\Kripanand\anaconda3\lib\site-packages\cufflinks\plotlytools.py:849: FutureWarning: The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead C:\Users\Kripanand\anaconda3\lib\site-packages\cufflinks\plotlytools.py:850: FutureWarning: The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead
histogram¶
In [25]:
df.iplot(kind='hist', bins=25, barmode='stack', theme='solar')
bubbleplot¶
In [26]:
cf.datagen.bubble3d(5, 6, mode='stocks').iplot(kind='bubble3d',
x='x',
y='y',
z='z',
size='size')
heatmap¶
In [27]:
cf.datagen.heatmap(17, 8).iplot(kind='heatmap',
colorscale='brbg',
title='Heatmap',
theme='solar')