Python Plotly Tutorial With Examples

Plotly ( as its URL goes), is a tech-computing company based in Montreal. It is known for developing and providing online analytics, statistics and graphing tools for individuals or companies. It also develops/provides scientific graphing libraries for Arduino, Julia, MATLAB, Perl, Python, R and REST.

Python Plotly Library

Plotly’s Python graphing library makes interactive graphs online and allows us to save them offline if need be.

Why Plotly

Plotly has got some amazing features that make it better than other graphing libraries:

  • It is interactive by default
  • Charts are not saved as images but serialized as JSON, making them open to be read with R, MATLAB, Julia and others easily
  • Exports vector for print/publication
  • Easy to manipulate/embed on web

Getting Started

We need PIP (python package installer) to get working with plotly, we’ll also need to create an account with plotly in case we want to use the online facilities. For this lesson, we’ll stick to offline usage only.


To install plotly, open a terminal window and run the following command:

This may take a few minutes to install to collect dependencies and download them:


Using Plotly

To use plotly in any of the Python scripts, we will need to import plotly as:

A few more steps and you are ready to use plotly. For Online usage, you need to set up credentials. This can be done as:

For offline usage, we need to call plot like the following for offline usage:

Test Installation

Before we can start using plotly, let’s test its installation with an easy script:

Let’s see the output for this program:
pyhton ploty
As expected, this returns the latest version of plotly.

Making a Simple Graph

Let’s start with a simple “Hello, World” program with a sample code snippet:

Let’s see the output for this program:
pyhton simple graph
As cleary visible, this graph is saved as an HTML file in the same directory as the script.

Basic Charts

To start visualising data, we will start with Basic Charts using Plotly and then move to more complex examples which shows time-related plotting.

Scatter Plot

We’ll create a basic chart based on some random data using numpy. If numpy isn’t installed on your machine, install it using this command:

Here is a sample program to show a scatter plot:

Let’s see the output for this program:
pyhton scatter graph
In this script, we also provided a name for the HTML file.

Line and Scatter Plot

We can create some more sophisticated/ informative plots such as Line Scatter plot in a similar manner as above:

Let’s see the output for this program:
python line scatter graph
This is not it. This graph is much more informative than it looks right now. Move the mouse pointer to any of the plotted point and you’ll see more information about that point:

python line scatter graph

Box Plots

Box Plots are quite informative and helpful especially when you have too much to show from very little data. Let’s try and create one:

Let’s see the output for this program:
plotly box plot
Again, we moved the mouse point to one of the point to explore more information about that point.

Contour Plots

Contour Plots are one of most commonly used scientific plots:

Let’s see the output for this program:
plotly box plot
These kind of plots are used a lot while showing heat map data.

Financial Charts

Financial Charts are mycg more complex to read but are easy to make with Plotly. Let;s see some types of the charts taht can be made with Plotly.

Time Series Plot

Let’s start with a Time Series Plot. We will make use of sample data by Plotly itself which served by Github Repository. Here is a sample program:

Let’s see the output for this program:
plotly box plot
If pandas isn’t installed on your machine, install it using this command:

OHLC Chart

OHLC Charts are also an excellent way to explain time-series related data for non-uniform distribution. Let’s look at a code snippet:

Let’s see the output for this program:



In this lesson, we studied another excellent Python library which is used for visualisation and offline usage.

Read more Machine Learning posts here.

By admin

Leave a Reply