Python

Plotly.Graph_objects.waterfall

“Waterfall plots are a common type of plot that resembles histograms. Like histograms, they are used to show the change in data over time.

In this article, we will explore how to create waterfall plots using the Plotly graph_objects module.”

Let’s dive in.

Basic Waterfall Chart

The code provided below illustrates how to create a simple waterfall plot using graph_objects.Waterfall function.

import plotly.graph_objects as go
stage = ['intial', 'dependencies', 'database', 'caching', 'post_production', 'refactor', 'production']
compile_time = [1.2, 13.11, 15.67, 11.91, 23.21, 13.31, 8.88 ]
fig = go.Figure(data=go.Waterfall(
    x=stage,
    y=compile_time
))
fig.show()

The code above will plot a waterfall chart showing the compile time of a project over various stages.

The resulting figure is as shown:

Specifying Plot Orientation

We can specify the orientation of the figure using the orientation parameter as shown below:

import plotly.graph_objects as go
stage = ['intial', 'dependencies', 'database', 'caching', 'post_production', 'refactor', 'production']
compile_time = [1.2, 13.11, 15.67, 11.91, 23.21, 13.31, 8.88 ]

fig = go.Figure(data=go.Waterfall(
    y=stage,
    x=compile_time,
    orientation='h',
))
fig.show()

This will set the figure orientation to horizontal as shown:

To set the title for the figure and show the legend, you can use the update_layout function as shown:

import plotly.graph_objects as go
stage = ['intial', 'dependencies', 'database', 'caching', 'post_production', 'refactor', 'production']
compile_time = [1.2, 13.11, 15.67, 11.91, 23.21, 13.31, 8.88 ]
fig = go.Figure(data=go.Waterfall(
    y=stage,
    x=compile_time,
    orientation='h',
))
fig.update_layout(title = 'compile time at different stages', showlegend=True)
fig.show()

The output figure is as shown:

Closing

In this article, we discussed the fundamentals of creating waterfall charts using the Plotly graph_objects module.

Thanks for reading & Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list