MITB Banner

Beginner’s Guide To Data Visualisation With Plotly & Cufflinks

Share

Vocabulary Builder

Everyone in this industry understands the importance of data visualisation and the fact that it is a powerful skill which can be used to tell interesting stories from data. In our previous articles, we have also talked about some of the popular visualisation libraries in Python such as Seaborn and Pandas inbuilt visualisation. In this tutorial, we will introduce you to Plotly and Cufflinks which are popular for their interactive and beautiful plots.

Plotly & Cufflinks

Plotly is an open-source and browser-based graphing library which facilitates interactive plotting.  The library is available for a number of programming languages such as Python, R, MATLAB, Perl, Julia, Arduino, and REST, among others.

Cufflinks is another library that connects the Pandas data frame with Plotly enabling users to create visualizations directly from Pandas. The library binds the power of Plotly with the flexibility of Pandas for easy plotting.

Visualising Data With Plotly & Cufflinks

Installing Plotly and Cufflinks

To install Plotly and cufflinks type and execute the following commands

pip install plotly
pip install cufflinks

Note:

If you are using the Anaconda distribution make sure to activate your conda environment using the conda activate command before executing the above commands. Use conda deactivate to exit the conda environment.

Hands-On Visualisation

For those who have followed the Pandas plotting, this may sound fairly simple as all we need to do is import some libraries and rename the plot function of Pandas as iplot which stands for interactive plot. Let’s look at some examples.

Importing necessary libraries

For iplots, you might want to stick to using Jupyter Notebook as it has great support for it.

import plotly
import cufflinks as cf
import pandas as pd
import numpy as np

#Enabling the offline mode for interactive plotting locally
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
init_notebook_mode(connected=True)
cf.go_offline()

#To display the plots
%matplotlib inline

Importing The Dataset

Consider a simple dataset of random numbers. You can either create your own download the same sample used in the below examples by clicking here.

data = pd.read_csv("sample_data.csv")

This is what the dataset looks like :

Creating Interactive Plots

Let’s do a simple plot of the numerical features in the dataset which are columns A, B and C.

data[['A','B','C']].iplot()

As you can see in the above image, Plotly provides a set of options to play around with the visualisation. On the top right corner of the generated plot, you can see 10 buttons that offer different functionalities. In the order of buttons from going left to right we have one to download the plot as a png image, toggle buttons to switch between zoom mode and pan mode, a zoom in and zoom out button, an autoscale button to reset the scale in the plot, a reset axis button, a button to toggle the spike lines in the graph, and pair of toggle button to switch between different mouse hover modes.

On the bottom right corner of the image, we also get an option to export the graph to Plotly’s online gallery.

Like the Pandas plot function, iplot enables users to use the ‘kind’ keyword argument to plot different kinds of plots such as scatter plots, bar plots, histograms etc. Let’s look at some examples.

Scatter plot :

data[['A','B','C']].iplot(kind = 'scatter' , mode = 'markers')

  • kind = “scatter” specifies that we need a scatter plot.
  • mode = “markers” sets the markers(Otherwise the plot will use lines by default).

You can also pass in the axis variables x and y as arguments, for example, x = ‘A’ sets column A along the X axis.

There is a wide range of arguments that can be passed to the iplot method to customise the visualisations. To understand  the full list of arguments click here to head to the official documentation of Pyplot for Python,

Bar Graph:

data.iplot(kind = 'bar', x = 'D', y = 'C')

Surface Plots: 

Plotly also allows users to plot 3D interactive plots.

data[['A','B','C']].iplot(kind = 'surface', colorscale = 'blues')

The above code displays the numerical features in the dataset along x,y and z-axis in a 3 Dimensional plane.

Outlook

That was a mere gentle introduction to Pyplot and its capabilities don’t stop here. With almost all sorts of customisations available for each kind of plot, there is no limit for creativity with Pyplot. Pyplot can interactively plot all kinds of charts that are available with Pandas plus many more beautiful visualisations.

Share
Picture of Amal Nair

Amal Nair

A Computer Science Engineer turned Data Scientist who is passionate about AI and all related technologies. Contact: amal.nair@analyticsindiamag.com
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.