#!/usr/bin/env python # coding: utf-8 # # Plotly and Cufflinks # In[108]: import pandas as pd import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') # In[109]: from plotly import __version__ # In[110]: print(__version__) # In[111]: import cufflinks as cf # In[112]: from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot # In[113]: init_notebook_mode(connected=True) # In[193]: cf.go_offline() # In[247]: # Some random data df = pd.DataFrame(np.random.randn(800,4), columns='Sabrina Rose Lusi Fiona'.split()) # In[248]: df.head() # In[251]: get_ipython().run_line_magic('matplotlib', 'inline') # In[252]: # Using Interactive Image from Cufflinks df.iplot() # In[255]: df.iplot(kind='scatter', x='Sabrina', y='Rose', title = 'Friendship Scatter Plot', mode='markers', size=20) # Two of my besties randomly scattered. # In[207]: # Histogram for all variables in the dataset df.iplot(kind = 'hist', title='Frienship Histogram', bins=50) # In[256]: df.sum().iplot(kind='bar', title='Cumulative Friendship') # In[257]: # Boxplot of Friendship df.iplot(kind='box', title='Box Plot of Friendship') # In[209]: # Scatter Matrix df.scatter_matrix() # In[208]: # Spread type visulization df[['Rose', 'Sabrina']].iplot(kind='spread') # In[281]: # Some more random data df2 = pd.DataFrame({'Partner':['Rose', 'Lexie', 'Fiona'], 'Scores':[200, 120, 150]}) df2 # In[254]: # Barplot to show the coolness of my friends df2.iplot(kind='bar', x='Partner', y='Scores', title='Friendship Score') # Now it is apparent that Rose is the boss! Love Fiona -- the best cat friend ever! Lexie is actually just imaginary. # In[253]: df3 = pd.DataFrame(np.random.rand(10, 4), columns=['Lilo', 'Stitch', 'Moana', 'Maui']) print(df3) df3.iplot(kind='barh',barmode='stack') # In[280]: df3.iplot(kind='area', fill=True) # In[258]: # 3D Surface plot df3.iplot(kind='surface', colorscale='Set1', title='Disney Battle') # In[219]: # Import my mortality rate dataset import pandas as pd study = pd.read_csv('study1.csv') study.head(5) # In[284]: # Scatter Plots study.iplot(kind='scatter', mode='markers', x='GDPPerCapita', y='lifeExpectancy', xTitle='GDP per Capita', yTitle='Life Expectancy', title='GDPpc VS Life Expectancy 2010') # In[245]: study.iplot(kind='bubble', x='GDPPerCapita', y='lifeExpectancy', size='totalPopulation', text='Country', categories='lifeExpectancy', showlegend=False, colorscale='Set1', xTitle='GDP per Capita', yTitle='Life Expectancy', title='GDPpc VS Life Expectancy 2010') # Interesting to see that life expectancy improves faster when GDP per Capita is lower (steeper positive slope). When GDPpc hits a certain point like China and India, improvement of life expectancy slows down. The colors of the bubbles are based on life expectancy. # In[243]: study.iplot(kind='bubble', x='totalFertilityRate', y='underFiveMortality', size='totalPopulation', text='Country', categories='lifeExpectancy', showlegend=False, colorscale='Set1', xTitle='totalFertilityRate', yTitle='underFiveMortality', title='Total Fertility Rate VS Child Mortality Rate 2010') # Interesting to see that there is a positive correlation between fertility and child mortality. # In[273]: study.iplot(kind='bubble', x='womenMeanYearsInSchool', y='underFiveMortality', size='totalPopulation', text='Country', categories='lifeExpectancy', showlegend=False, colorscale='Set1', xTitle='womenMeanYearsInSchool', yTitle='underFiveMortality', title='Women Mean Years In School VS Child Mortality Rate 2010') # Women who are more educated are more likely to know how to take care of their child/children. # In[277]: study.iplot(kind='bubble', x='agriPercentGDP', y='underFiveMortality', size='GDPPerCapita', text='Country', categories='lifeExpectancy', showlegend=False, colorscale='Set1', xTitle='agriPercentGDP', yTitle='underFiveMortality', title='Agriculture Percent GDP VS Child Mortality Rate 2010') # Note that in this graph, the bigger the bubble the higer the GDPpc. The big bubble countries have lower percentage of agriculture in their GDP and tend to have lower child mortality rate. On the other hands, the smaller bubble countries with higher percentage of agriculture tend to have higher child mortality rate. # In[288]: # Heatmap to study correlations study.corr().iplot(kind='heatmap', colorscale='spectral')