Funnel charts in Python using Plotly

Funnel Charts are often used to represent data in different stages of a business process. It’s an important mechanism in Business Intelligence to identify potential problem areas of a process. For example, it’s used to observe the revenue or loss in a sales process for each stage.

In this post, we’ll learn how to plot a funnel chart using a numerical dataset.

We are going to use a sample dataset from a dummy E-commerce firm’s social media campaign. The funnel chart will represent the flow of new users at different stages of the campaign.

These are the five stages of user flow:

Link Visit : When a user clicks on the campaign link

Sign-up : When a user creates an account

Selection : When a user adds a product to the cart

Purchase : When a user buys a product

Review : When a user reviews a purchased product

Here is the table (dataset) containing values (number of users) for all the intermediate phases.

We will use Plotly shapes to draw the sections of a funnel. Each funnel section will be represented by a Quadrilateral (4 sided polygon).

A section will be a Rectangle if it has value equal to its next phase-value, or it’ll be a Isosceles Trapezoid (Isosceles Trapezium in British English) if its value is unequal to the next phase’s value.

We are using a fixed width for the plot and the section (phase) having the maximum users (value). All other sections will be drawn according to their values relative to the maximum value.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

n_phase=len(phases)

# the fixed width for the plot

plot_width=400

# height of a section and difference between sections

section_h=100

section_d=10

# multiply factor to calculate the width of other sections

unit_width=plot_width/max(values)

# width for all the sections (phases)

phase_w=[int(value *unit_width)forvalue invalues]

Each section will have a height of 100px and there will be a difference of 10px in successive sections.