close
close
plotly sankey

plotly sankey

3 min read 16-10-2024
plotly sankey

Unveiling Data Flow with Plotly Sankey Diagrams

Sankey diagrams are powerful visual tools that excel at illustrating the flow of quantities through a process or system. Whether you're tracking energy usage, customer journeys, or financial transactions, Sankey diagrams offer a clear and intuitive way to understand complex relationships. Plotly, a popular Python library for data visualization, provides a robust and customizable way to create stunning Sankey diagrams.

What are Sankey diagrams?

Imagine a system with different stages, where elements move from one stage to another. A Sankey diagram visually represents this flow using connected bands. The width of each band is proportional to the quantity flowing along that path. This visual representation allows you to quickly grasp:

  • The relative importance of different pathways: The wider a band, the more significant its flow.
  • The impact of changes: By comparing two Sankey diagrams, you can identify shifts in flow and their potential causes.
  • Potential bottlenecks or inefficiencies: Narrow bands indicate restricted flow, highlighting areas for improvement.

Creating Sankey Diagrams with Plotly

Plotly's plotly.graph_objects.Sankey class makes creating Sankey diagrams surprisingly simple. Let's break it down with an example.

Example:

import plotly.graph_objects as go

# Define the nodes (stages in the process)
nodes = [
    {"label": "Product A", "id": 0},
    {"label": "Product B", "id": 1},
    {"label": "Product C", "id": 2},
    {"label": "Sales Channel 1", "id": 3},
    {"label": "Sales Channel 2", "id": 4},
    {"label": "Sales Channel 3", "id": 5},
    {"label": "Revenue", "id": 6},
]

# Define the links (flow between nodes)
links = [
    {"source": 0, "target": 3, "value": 50},
    {"source": 0, "target": 4, "value": 30},
    {"source": 0, "target": 5, "value": 20},
    {"source": 1, "target": 3, "value": 40},
    {"source": 1, "target": 5, "value": 60},
    {"source": 2, "target": 4, "value": 70},
    {"source": 3, "target": 6, "value": 100},
    {"source": 4, "target": 6, "value": 100},
    {"source": 5, "target": 6, "value": 100},
]

# Create the Sankey diagram
fig = go.Figure(data=[go.Sankey(
    node=dict(label= [n['label'] for n in nodes], pad=15, thickness=20, line=dict(color="black", width=0.5)),
    link=dict(source= [l['source'] for l in links], target= [l['target'] for l in links], value= [l['value'] for l in links], color="lightblue")
)])

# Customize the layout (optional)
fig.update_layout(title_text="Product Flow to Revenue", font_size=16)

# Display the diagram
fig.show()

This code creates a Sankey diagram showing the flow of three products through different sales channels and ultimately to revenue. The color, width, and labels of the bands are easily customized to enhance clarity and visual appeal.

Beyond the Basics

Plotly's Sankey diagrams offer more than just basic visualization. Here are some advanced features:

  • Color Customization: Use different colors to represent different products, channels, or categories within the flow.
  • Interactive Elements: Add hover tooltips, click events, and annotations to provide detailed information about each flow path.
  • Multiple Diagrams: Combine multiple Sankey diagrams side-by-side to compare different scenarios or time periods.
  • Animation: Illustrate dynamic changes in flow over time by animating the diagram.

Real-world Applications

Sankey diagrams are versatile tools with diverse applications across various industries. They are especially useful in:

  • Supply Chain Management: Visualizing the flow of goods from raw materials to finished products.
  • Customer Journey Mapping: Understanding customer interactions and touchpoints throughout their journey.
  • Energy Efficiency: Analyzing energy flows in buildings or industrial processes.
  • Financial Modeling: Tracking the movement of funds and identifying potential risks.

Conclusion

Plotly Sankey diagrams offer a powerful and visually compelling way to represent complex data flows. By combining their flexibility with insightful analysis, you can gain a deeper understanding of your data and communicate key insights effectively. Whether you're a data scientist, analyst, or simply looking for a way to present your data clearly, mastering Sankey diagrams is a valuable skill to add to your arsenal.

Related Posts


Popular Posts