Plotly & Cufflinks | A Data Visualisation Library with Modern Features | Python | Data Science | Data Visualisation

Plotly & Cufflinks | A Data Visualisation Library with Modern Features | Python | Data Science | Data Visualisation

Plotly

Plotly gives you lots of interactive and dynamic data visualisation & UI tools for data science, Machine Learning & Engineering. It has some great features which we will discuss in this post. So, be ready for the tutorial.

Installation Guide on your Machine:
For Plotly put this on your terminal and run.
pip install plotly
or
conda install -c plotly plotly

Cufflinks

For using Plotly library we have to configure it with the pandas, so the cufflinks library giver us a privilege to do it. Because the Plotly is built on the top of d3.js.

Installation Guide on your Machine:
For cufflinks put this on your terminal and run.
pip install cufflinks
or
conda install -c conda-forge cufflinks-py

Let's begin...

Import all the Libraries needed.
import pandas as pd
import numpy as np
import cufflinks as cf
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot

Line 1: Importing Pandas for making data frame.
Line 2: Importing Numpy, will help us to do some maths.
Line 3: Importing cufflinks for connecting pandas and Plotly.
Line 4: From Plotly's offline version we're importing four classes which we will use to make some interactive visualisations.

init_notebook_mode(connected = True)

Line 5: It will connect your notebook to your javascript, it will allow your notebook to access visualisations.

cf.go_offline()

Line 6: It will allow your cufflinks, to go offline. 

df1=pd.DataFrame(np.random.randn(50,5),columns='A B C D E'.split())
print(df1.head())

Line 7: Making a data frame, with random numbers, of size 100x5, naming columns as A, B, C, D, E. Let's have a look at the data.

Line 8: Printing that Data frame.

OUTPUT: df1.head()


df2=pd.DataFrame({'Category':['A','B','C'],'Values':[43,21,34]})
print(df2)

Line 9: Creating a data frame with the help of a dictionary. Let's have a look at the data.

Line 10: Printing that data frame.

OUTPUT: df2

print(df1.plot())

Line Graph

Line 11: Plotting the line graph with the help of pandas' inbuilt method.

OUTPUT: df1.plot()
OUTPUT: df1.plot()
This plot shows you the line plot of ABCDE, and you have a mess all around your screen. It is very hard to read. Here the concept of Plotly comes which will give a number of useful options to do with your graph.

adv_plot=df.iplot()
print(adv_plot)
Line 12: *Important* By using plot() method we can get an advance control per the graph like you can zoom in a selected area to see the insight of this mess. You can hide/unhide any line from the legend window. You can hover your cursor over this graph, you can compare any real-time values at any point and whatnot. You can even download the graph. You can easily observe from the plot below.

OUTPUT: adv_plot


Scatter Plot

scatter_plot = df.iplot(kind='scatter',x='A',y='C',mode='markers',theme='space')
print(scatter_plot)

Line 13: Printing a scatter plot, by Plotly. You can change the type of kind to print different types of plot. The available values of 'kind' are { 'scatter', 'bar',  'box',  'spread',  'ratio',  'heatmap', 'surface',  'histogram',  'bubble',  'bubble3d',  'scatter3d',   'scattergeo',  'ohlc',  'candle',  'pie', 'choroplet' }. You can also change the themes as per your requirement. Available themes are: [ 'ggplot', 'pearl', 'solar', 'space', 'white', 'polar', 'henanigans' ].

Line 14: Printing the plot.

OUTPUT: scatter_plot



box_plot =  df.iplot(kind='box',theme='pearl')
print(box_plot)
Line 15: Making a box plot.

Line 16: Printing the box plot.

OUTPUT: box_plot


Surface 3D Plot

df3=pd.DataFrame({'x':[1,2,3,4,5],'y':[10,50,30,20,30],'z':[500,100,600,300,200]})
3D_plot = df3.iplot(kind='surface', colorscale='paired' )
print(3D_plot)

Line 17: Making a data frame from dictionary of three columns for the 3D plot.

Line 18: With the help of 'iplot', a surface 3d plot is assigned to 3D_plot.

Line 19: Printing 3D plot.

OUTPUT: 3D_plot

Spread plot

spread_plot = df[['B','C','D']].iplot(kind='spread')
print(spread_plot)

Line 20: A spread plot is a plot which shows you the spread between multiple values, at every single point in your data set, here is the code for spread plot.

Line 21: Printing a spread plot.

OUTPUT: spread_plot

Bubble Plot

bubble_plot=df.iplot(kind='bubble',x='A',y='B',size='C', color='purple')
print(bubble_plot)
Line 22: Bubble plot is same as the scatter plot but in this case, the size of bubbles varies with respect to the other column. Let's say for example, here size='C'

Line 23: Printing the bubble plot.

OUTOUT: bubble+plot


Conclusion:
For this tutorial, we are stopping here, but the potty itself has lots of features and it can make a lot more plots. They all are can't be covered in a single tutorial, so go and check out the documentation for more. The aim of this tutorial was to make you enough aware of this type of library.

If you love my work then you can connect me on LinkedIn and Github.








Comments

Post a Comment

Popular posts from this blog

SMART HOSPITAL | IoT based smart health monitoring system | Real time responses | Internet Of Things | ESP8266

A Quick Guide to Data pre-processing for Machine Learning | Python | IMPUTATION | STANDARDISATION | Data Analysis | Data Science