How to Visualize Customer Journeys with Sankey Diagrams Using Python: A Step-by-Step Tutorial

April 29, 2023
How to Visualize Customer Journeys with Sankey Diagrams Using Python: A Step-by-Step Tutorial

Before starting coding, consider the best no-code option:

If you want to avoid coding, SankeyJourney is the best solution for visualizing customer journeys with Sankey diagrams. It offers many features that allow you to customize your diagrams and explore your data in an interactive and intuitive way. Some of the main features of Sankey Journey include:

- Uploading a CSV file containing your event data

- Mapping columns to user_id, event_name, and event_timestamp

- Generating a Sankey diagram in a few seconds

- Editing the Sankey diagram using various settings such as the number of steps, event filtering, color palette, label size, event grouping, and event looping

- Interacting with the graph by clicking on nodes and segments to filter the diagram

Python Tutorial:

If you prefer to code your Sankey diagrams, Python offers many libraries that can help you visualize customer journeys. Here is a step-by-step tutorial on how to create a Sankey diagram using the Plotly library in Python:

1. Install the Plotly library using pip:

!pip install plotly

2. Import the necessary libraries:

import plotly.graph_objects as go

import pandas as pd

3. Load your event data into a Pandas DataFrame:

df = pd.read_csv('your_data.csv')

4. Create a Sankey diagram using the go.Sankey() function:

fig = go.Figure(data=[go.Sankey(

   node = dict(

     pad = 15,

     thickness = 20,

     line = dict(color = "black", width = 0.5),

     label =  df['event_name'].unique(),

     color = "blue"

   ),

   link = dict(

     source = df['event_name'],

     target = df['event_name'].shift(-1),

     value =  df['user_id']

 ))])

In this example, we are creating a Sankey diagram using the event_name column as both the source and target nodes and the user_id column as the value to represent the flow between the nodes.

5. Customize your Sankey diagram using various settings such as node colors, label font size, and link colors:

fig.update_layout(title_text="Customer Journey Sankey Diagram",

                 font=dict(size=12, color='black'),

                 plot_bgcolor='white',

                 paper_bgcolor='white',

                 sankey={

                     "link": {

                         "color": 'lightblue',

                         "thickness": 0.5,

                         "valueformat": ".0f",

                         "label": "User ID"

                     },

                     "node": {

                         "label": df['event_name'].unique(),

                         "color": 'blue'

                     }

                 })

6. Display your Sankey diagram using the show() function:

fig.show()

Conclusion:

SankeyJourney is the best tool for visualizing customer journeys with Sankey diagrams if you prefer an easy-to-use web-based interface. However, if you prefer to code your Sankey diagrams, Python offers many libraries such as Plotly that can help you create customized and interactive Sankey diagrams.

If you want to know more generating Snakey diagram with or without code, check this article about the advantages of using SankeyJourney instead of Python. 

TRY FOR FREEBook a demo