1. Home
  2. Docs
  3. Advanced Python
  4. Data Visualization
  5. Plotly and Cufflink

Plotly and Cufflink

Plotly and Cufflink

  • Plotly is an open-source and charting library that provides the facility of interactive plotting
  • Plotly charts are dynamic in nature which allows to hover over the values, zoom in, zoom out of graphs and identifies outliers in the dataset
  • Cufflink is also a python library that connects plotly with pandas so that we can create charts directly on data frames. It acts as a plugin

Installation

plotly

conda install plotly
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

Note: you may need to restart the kernel to use updated packages.## Package Plan ##

  environment location: C:\Programme\Anaconda3

  added / updated specs:
    - plotly

The following packages will be SUPERSEDED by a higher-priority channel:

  conda              conda-forge::conda-4.12.0-py38haa244f~ --> pkgs/main::conda-4.12.0-py38haa95532_0

Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done

Cufflink

conda install -c conda-forge cufflinks-py
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

## Package Plan ##

  environment location: C:\Programme\Anaconda3

  added / updated specs:
    - cufflinks-py

Note: you may need to restart the kernel to use updated packages.
The following packages will be SUPERSEDED by a higher-priority channel:

  conda              pkgs/main::conda-4.12.0-py38haa95532_0 --> conda-forge::conda-4.12.0-py38haa244fe_0

Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done

Importing plotly and cufflink

import plotly.offline as pl
import cufflinks as cf
import pandas as pd
from plotly.offline import iplot
cf.go_offline() #will make cufflinks offline
cf.set_config_file(offline=False, world_readable=True)

Histogram

To get the count of range of data

l1 = [250,370,470,420,520,560,225,900,430,330,315]
l2 = [12,16,8,9,10,10,14,8,18,15,13]
bulb = pd.DataFrame({'Life Time':l1,'number of bulbs':l2}
                   )
bulb['Life Time'].iplot(kind="histogram",theme="white",title="LifeTime of bulb",xTitle='LifeTime',yTitle='Count')

Heatmap

Often Heat map is used to print the correlation between variables

data= pd.read_csv("F:/SRIHER/2021-2022/Quarter - 3/Advanced Python/Module - 3/Dataset/tips.csv")
data.corr().iplot(kind='heatmap',colorscale="Blues",title="Feature Correlation Matrix")

To Get Help

help(data.iplot)
Help on method _iplot in module cufflinks.plotlytools:

_iplot(kind='scatter', data=None, layout=None, filename='', sharing=None, title='', xTitle='', yTitle='', zTitle='', theme=None, colors=None, colorscale=None, fill=False, width=None, dash='solid', mode='', interpolation='linear', symbol='circle', size=12, barmode='', sortbars=False, bargap=None, bargroupgap=None, bins=None, histnorm='', histfunc='count', orientation='v', boxpoints=False, annotations=None, keys=False, bestfit=False, bestfit_colors=None, mean=False, mean_colors=None, categories='', x='', y='', z='', text='', gridcolor=None, zerolinecolor=None, margin=None, labels=None, values=None, secondary_y='', secondary_y_title='', subplots=False, shape=None, error_x=None, error_y=None, error_type='data', locations=None, lon=None, lat=None, asFrame=False, asDates=False, asFigure=False, asImage=False, dimensions=None, asPlot=False, asUrl=False, online=None, **kwargs) method of pandas.core.frame.DataFrame instance
           Returns a plotly chart either as inline chart, image of Figure object
    
           Parameters:
           -----------
                   kind : string
                           Kind of chart
                                   scatter
                                   bar
                                   box
                                   spread
                                   ratio
                                   heatmap

Box Plot

The sale of electric bulb on different days of a week is shown below,

daysnumber of electric bulbs (Week 1)number of electric bulbs (Week 2)
Monday1210
Tuesday168
Wednesday814
Thursday109
Friday148
Saturday820
Sunday1822

Represent the above data with the help of box plot for week1 and week2

sales = pd.DataFrame({'Days':['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
                     'Week1':[12,16,8,10,14,8,18],
                     'Week2':[10,8,14,9,8,20,22]}
                    )
sales[['Week1','Week2']].iplot(kind='box')

Specify the column names on which to draw box plot

Scatter plots

Draw on two Quantitative variables

data= pd.read_csv("F:/Advanced Python/Module - 3/Dataset/iris.csv")
data.iplot(kind="scatter", theme="pearl",x="Sepal Length",y="Petal Length",
            categories="Class",xTitle="Sepal Length", yTitle="Petal Length", title="Scatter on Class")

Bar Plot

In tips data set, plot the bar of how many smokers are male and female. Similarly how many non smokers are male ad female.

data1= pd.read_csv("F:/SRIHER/2021-2022/Quarter - 3/Advanced Python/Module - 3/Dataset/tips.csv")
data1
 total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.5MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
……………………
23929.035.92MaleNoSatDinner3
24027.182FemaleYesSatDinner2
24122.672MaleYesSatDinner2
24217.821.75MaleNoSatDinner2
24318.783FemaleNoThurDinner2
smoker_sex = data1[data1['smoker']=='Yes']['sex'].value_counts()
nonsmoker_sex = data1[data1['smoker']=='No']['sex'].value_counts()
data2 = pd.DataFrame([smoker_sex,nonsmoker_sex])
data2.index = ['Smoker','Non Smoker']
data2.iplot(kind='bar', title='Habit of Smoking by the Sex')

Pie chart

The colours preferred by the group of people are,

ColoursNumber of people
blue12
green16
yellow8
Orange10
red14

Represent the above data with the help of pie plot

import plotly.express as px
 
fav = pd.DataFrame({'Colours':['blue','green','yellow','Orange','red'],
                     'Number of people':[12,16,8,10,14,]
                    })
fig = px.pie(fav,values="Number of people",names="Colours",
             title='Favourite color of the people')
fig.show()

#fav.iplot(kind='pie', title='Favourite color of the people',values=fav['Number of people'],labels='Colours')
#pl.pie(values=fav['Number of people'],labels='Colours')

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments