Python Plotly Tutorial

Python Plotly Library

Python Plotly library serves the purpose of Data Visualization. It helps in creating interactive, best-quality graphs online and can save them offline as well.

Need for Plotly in Python

Plotly is useful in the field of statistical analysis, data visualization, etc. The outcome of the analysis and predictions can be presented in vivid forms using Plotly.

Getting started with Python Plotly

In order to utilize the Plotly library, we first need to install it using the pip command.


pip install plotly==4.5.0
Installation Of Plotly
Installation Of Plotly

Basic Charts in Python Plotly

Let’s dive into the functionalities served by the Plotly library of Python. This section covers some of the Basic Plotting techniques to serve the purpose of Data Visualization.

1. Scatter plot

As the name suggests, it represents the data in a scattered format.

We have used NumPy to generate random values to be passed as input to the graph.


import plotly
import plotly.graph_objs as G

import numpy

num = 15
x = numpy.random.randn(num)
y = numpy.random.randn(num)

# Create a trace
follow = G.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers'
output = [follow]

plotly.offline.plot(output, filename='basic-scatter.html')

The plotly.graph contains JSON object which is a dict like structure. By updating values of few keywords of this object, vivid kinds of graphs can be plotted.

In the above snippet, plotly.graph’s JSON object is represented as G. Further, we have used NumPy to generate random values for the sake of providing input and plotting of data.

object.Scatter() is used to provide dimensional values i.e. create a trace and is useful to set other attributes that we feel like adding to the graph.

The x and y parameters contain the values to be plotted on the x and y-axis.

The parameter mode determines the mode of representation of Scatter Plot. It can have any of the following values:

  • lines
  • markers
  • lines+markers
  • lines+markers+text
  • none

lines plot values through lines as a drawing mode.

markers would plot value by marking the un-segregated data as points.

plotly.offline enables the programmer to plot the values in an offline manner and save it. It accepts a filename as an argument which is the .html page that would display the offline plotting of the data.


Scatter Plotly
Scatter Plotly

2. Line-Scatter plot

In this type of plotting, a combination of line and scattering fashion is used to represent the data.

import plotly
import plotly.graph_objs as G

import numpy as p

N = 20
x = p.linspace(0, 1, N)

# Create traces
plot0 = G.Scatter(
    x = x,
    y = one_y,
    mode = 'markers'

plot1 = G.Scatter(
    x = x,
    y = two_y,
    mode = 'lines+markers'

plot2 = G.Scatter(
    x = x,
    y = three_y,
    mode = 'lines'

output = [plot0, plot1, plot2]
plotly.offline.plot(output, filename='line-scatter.html')

In the above snippet of code, we have used numpy.linespace() function to generate evenly spaced values for the x dimension.

Further, we have used numpy.random() function to generate random values for three different traces through y-axis.

As seen above, we have passed different values to the parameter mode, representing the type of drawing fashion. line+markers represents the values and plots them with a combination of lines and marker dots.


Line Scatter Plot
Line Scatter Plot

3. Bubble Scatter Plot

import plotly.graph_objects as G

img = G.Figure(data=G.Scatter(
    x=[10, 20, 30, 40],
    y=[5, 10, 15, 20],
                color=[1, 2, 3, 4])

marker is a dict that sets the symbol for representing the data. size is used to pass the dimensions to the drawing symbol and color is used to set values to add color to those drawing symbols.

The plotly.Figure() function basically contains data and the drawing layout and it combines both of these values to create a figure. The data and layout values can be represented as graph objects or dict.

The function is used to plot the figure along with its layout design.


Bubble Scatter Plot
Bubble Scatter Plot

Statistical Style Charts

These kinds of charts are helpful in displaying the data in a much simplified manner.

1. Box Plot

import plotly.graph_objects as G
import numpy

a = numpy.random.randn(100) - 10
b = numpy.random.randn(100) + 10

output = G.Figure()

plotly.add_trace() function is used to update the graph by adding traces to the x and y dimensions. It accepts a graph object to be traced as a parameter i.e. G.Scatter, G.Box, etc.

plotly.graph.object.Box() basically sets the tracing values to the particular dimension.


Box Plot
Box Plot

2. Histogram

import plotly.graph_objects as G

import numpy as N

x = N.random.randn(100)

output = G.Figure(data=[G.Histogram(x=x)])

plotly.graph.object.Histogram() is used to construct a Histogram. x=x specifies the growth of the histogram on y-axis and vice versa.



3. DistPlots

The Distplot helps us plot un-distributed data and enables us to observe or inspect the values through the line plot.

import plotly.figure_factory as ff
import numpy as N

x = N.random.randn(500)
data = [x]
label = ['DISTPLOT'] 

output = ff.create_distplot(data, label)

Python’s API contains figure factory module to plot the data in a simplified manner.

figure_factory.distplot() plots the data as represents it as a combination of the histogram, normal curve, etc. The label parameter is used to set a text label to the graph.



Scientific Charts

These charts help in the analysis of scientific values or data from a wider perspective.

1. Counter Plots

Counter Plots are basically used in the scientific analysis of the huge amount of data together.

import plotly.graph_objects as G
from plotly.subplots import make_subplots

z =   [[2, 4, 7, 12, 13, 14, 15, 16],
       [3, 1, 6, 11, 12, 13, 16, 17],
       [4, 2, 7, 7, 11, 14, 17, 18],
       [5, 3, 8, 8, 13, 15, 18, 19],
       [7, 4, 10, 9, 16, 18, 20, 19],
       [9, 10, 5, 27, 23, 21, 21, 21],
       [11, 14, 17, 26, 25, 24, 23, 22]]

out = make_subplots(rows=1, cols=1)


plotly.subplots module enables the creation of numerous subplots of the data using the make_subplots() function.

The plotly.graph.objects.Contour() is used to create contour lines from the input array provided.


Counter Plots
Counter Plots

2. Heatmaps in Plotly

In Heatmap Plotting, each value passed to the input is represented as a pixel. On similar lines, Heatmaps too can be used to enhance the analysis of scientific values and research.

import plotly.graph_objects as G

out = G.Figure(data=G.Heatmap(
                    z=[[10, 20, 30],
                      [20, 30, 40],
                      [40, 50, 60]]))

The plotly.graph.obejct.Heatmap() function basically represents each value of the input data as Heatmap pixel.



Financial Plots

These can be considered as one of the most complex charts to form and depict the real-time analysis in a better manner.

1. Time-Series Chart

In the below snippet of code, we have used Pandas Module to read the CSV file and then have plotted the time-series chart for the same.

import as px
import pandas as pd

df = pd.read_csv('C:\\Users\\HP\\Desktop\\output11.csv')

fig = px.line(df, x='Marks', y='Sr no')

The file I’ve used is a simple file with two columns, serial number (sr no) and marks. The data is plotted automatically based on which axis uses which column of data. You can try the same with any two-column data CSV file.

Csv File
Csv File Snapshot


Time Series
Time Series

Alternatively, you can simply provide the data in the form of two lists as shown below. I’m providing some random values to show how the line charts form.

import as px
sr = [1,2,3,4,5,6]
marks = [20, 10, 50, 30, 100, 75]

fig = px.line(x=marks, y=sr)


Graph Plotly
Plotly Graph

The package is used to provide high quality and simplified graphs overall. function is used to plot a line according to the provided values and labels to the x and y dimensions.

2. Funnel Charts

Funnel charts enable us to represent the data in the different forms of stages resembling the business development process.

import as p
data = dict(
    num=[25, 50, 75, 100],
    work=["Requirement Analysis", "Design", "Modelling and Construction", "Testing and Deployment"])
out = p.funnel(data, x='num', y='work')

express.funnel() function represents every row of the input DataFrame as a stage of the funnel. Here, the input num and work are represented in the form of funnel structure.


Funnel Charts
Funnel Charts

As seen above, the chart depicts the stages of the development and the values associated with it.

3-D Charts

import plotly.graph_objects as G
import numpy as N

n = 100

figure = G.Figure(data=[G.Mesh3d(x=(55*N.random.randn(n)),

plotly.graph.object.Mesh3d() represents the data as a 3-D drawing structure having vertices x, y, z.


3 D Plot
3 D Plot


Thus, in this article, we have understood the functions served by Python’s Plotly library.