#!/usr/bin/env python # coding: utf-8 # # Installing Necessary Libraries # In[1]: import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt get_ipython().run_line_magic('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__) # In[10]: print(cf.__version__) # ### Embedding Online Plots to the Jupyter Notebook # # In[4]: ctls.embed('https://plotly.com/~cufflinks/8') # ### 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() # In[7]: #different themes for plots in plotly cf.getThemes() # 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() # 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() # 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 # In[19]: #color scales for surface and heatmaps cf.colors.scales() # 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') # ## `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')