Create a Funnel Chart in Plotly Python

Funnel charts are excellent for visualizing data that represents stages in a process, such as sales funnels, conversion rates, or workflows. With Plotly using python, creating an interactive and visually appealing funnel chart becomes straightforward. In this blog, I’ll show you how to create a funnel chart using Plotly in Python, explain the code step-by-step, and highlight its practical applications.

Install Plotly

Before we begin, ensure that you have Plotly installed in your Python environment. You can install it using pip:

pip install plotly

Basic Funnel Chart Code

Here’s a simple example to create a funnel chart:

import plotly.graph_objects as go

# Define the data for the funnel chart
labels = ['Step 1: Visitors', 'Step 2: Signups', 'Step 3: Purchases', 'Step 4: Retention']
values = [1000, 600, 300, 150]

# Create the funnel chart
fig = go.Figure(go.Funnel(
    y=labels,  # Labels for the stages
    x=values,  # Values for each stage
    textinfo="value+percent initial",  # Display values and percentages relative to the first stage
    marker=dict(
        color=['#636EFA', '#EF553B', '#00CC96', '#AB63FA'],  # Custom colors for the stages
        line=dict(width=2, color='white')  # Add borders to the sections
    )
))

# Add title
fig.update_layout(title="Basic Funnel Chart", title_x=0.5)

# Show the chart
fig.show()

Explanation:

  1. Data Definition:
    • labels: Describes each stage of the funnel.
    • values: Specifies the number of entries at each stage.
  2. Funnel Chart:
    • The go.Funnel object defines the chart, where y is for labels and x is for values.
  3. Customization:
    • textinfo adds both values and percentage information to the chart.
    • marker specifies the colors and adds white borders to the sections.
  4. Interactive Chart:
    • Plotly creates interactive charts by default, so you can hover over elements to view details.

Funnel Chart with Percentage Calculations

Now, let’s enhance the chart by adding percentage calculations and improving customization:

import plotly.graph_objects as go

# Define the data for the funnel chart
labels = ['Step 1: Visitors', 'Step 2: Signups', 'Step 3: Purchases', 'Step 4: Retention']
values = [1000, 600, 300, 150]

# Calculate percentages for better annotations
total = values[0]
percentages = [f"{(value / total) * 100:.1f}%" for value in values]

# Create the funnel chart
fig = go.Figure(go.Funnel(
    y=labels,  # Labels for the stages
    x=values,  # Values for each stage
    textinfo="value+percent total",  # Show values and percentages relative to the total
    text=percentages,  # Custom percentage annotations
    marker=dict(
        color=['#636EFA', '#EF553B', '#00CC96', '#AB63FA'],  # Custom colors for the stages
        line=dict(width=2, color='white')
    )
))

# Add title and labels
fig.update_layout(
    title="Enhanced Funnel Chart",
    title_x=0.5,
    xaxis_title="Stage Values",
    yaxis_title="Process Stages"
)

# Show the chart
fig.show()

Enhancements Added:

  1. Percentage Calculations:
    • Manually calculated percentages for each stage relative to the total.
    • Added these percentages as custom text annotations for more precise details.
  2. Additional Customizations:
    • Chart titles and axis labels for better clarity.
    • More vibrant colors to make the chart visually engaging.

Saving the Funnel Chart as an Image

Plotly allows you to save the chart as an image for reports or presentations. To do this, you’ll need to install the kaleido library:

pip install kaleido

Add the following line to the end of your code to save the chart as a PNG file:

fig.write_image("funnel_chart.png")
print("Funnel chart saved as funnel_chart.png")

Practical Applications

Funnel charts are incredibly useful for:

  • Sales Funnels: Visualizing leads, conversions, and customer retention.
  • Marketing Campaigns: Tracking user engagement across different stages.
  • Workflow Processes: Analyzing step-by-step progress and drop-off rates.

Example Output

Using the data provided (“Visitors” to “Retention”), the enhanced funnel chart visually represents the decreasing values at each stage and provides percentage annotations, helping you easily identify where drop-offs occur.

Conclusion

By following this guide, you’ve learned how to create a basic and enhanced funnel chart in Plotly Python. These charts are not only visually appealing but also highly interactive, making them perfect for data analysis and presentations. Try it out with your own data and customize it further to suit your needs.

Related blog posts