import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
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
import cufflinks as cf
cufflinks
and iplot()
to create different plots¶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
print(cf.__version__)
0.17.3
ctls.embed('https://plotly.com/~cufflinks/8')
py.offline.init_notebook_mode(connected=True)
cf.go_offline()
import chart_studio
chart_studio.tools.set_credentials_file(username='PriyankaJha', api_key='EgKBYXoVIi8Aw32QlKrf')
lineplot
¶#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()
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 |
#different themes for plots in plotly
cf.getThemes()
['ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans']
df.iplot(theme='solar')
scatterplot
¶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
¶titanic = sns.load_dataset('titanic')
titanic.head()
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 |
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
¶df.iplot(kind='bar', bargap=0.4, barmode='stack', theme='solar')
#horizontal bar graph
df.iplot(kind='barh', bargap=0.4, theme='solar')
boxplot
¶df.iplot(kind='box', theme='solar')
areaplot
¶#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()
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 |
df2.iplot(kind='area', fill=True, theme='solar')
surfaceplot
¶df3 = pd.DataFrame({
'X': [10, 20, 30, 20, 10],
'Y': [10, 20, 30, 20, 10],
'Z': [10, 20, 30, 20, 10]
})
df3
X | Y | Z | |
---|---|---|---|
0 | 10 | 10 | 10 |
1 | 20 | 20 | 20 |
2 | 30 | 30 | 30 |
3 | 20 | 20 | 20 |
4 | 10 | 10 | 10 |
#color scales for surface and heatmaps
cf.colors.scales()
df3.iplot(kind='surface', colorscale='spectral')
# help(cf.datagen)
cf.datagen.sinwave(12, 0.15).iplot(kind='surface', colorscale='set1')
3Dsacatterplot
¶cf.datagen.scatter3d(2, 150, mode='stocks').iplot(kind='scatter3d',
x='x',
y='y',
z='z')
spreadplot
¶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
¶df.iplot(kind='hist', bins=25, barmode='stack', theme='solar')
bubbleplot
¶cf.datagen.bubble3d(5, 6, mode='stocks').iplot(kind='bubble3d',
x='x',
y='y',
z='z',
size='size')
heatmap
¶cf.datagen.heatmap(17, 8).iplot(kind='heatmap',
colorscale='brbg',
title='Heatmap',
theme='solar')