How to create a Cumulative Histogram in Plotly?

A cumulative histogram is a type of histogram that shows the cumulative distribution function (CDF) of a dataset. The CDF represents the probability that a random observation from the dataset will be less than or equal to a certain value. Cumulative histograms are useful when you want to compare the distribution of two or more datasets or when you want to visualize the proportion of data points that fall below a certain threshold.

Plotly is a Python library for creating interactive and publication-quality visualizations. It is built on top of the D3.js visualization library and provides a wide range of visualization types, including scatter plots, bar charts, and histograms. Plotly also supports interactive features like zooming, panning, and hover-over tooltips.

To create a cumulative histogram in Plotly, you will first need to load your data and use either Plotly Express or Plotly Graph Objects with the cumulative_enabled=True parameter.

Creating a Cumulative Histogram

A cumulative histogram shows the cumulative distribution function (CDF) of a dataset. Instead of showing the frequency of data points in each bin, it shows the cumulative frequency of data points up to that bin. This type of histogram can be created by setting the cumulative_enabled parameter to True when creating the histogram in Plotly.

Example 1: Vertical Cumulative Histogram

A vertical cumulative histogram displays the cumulative frequencies on the y-axis and the variable values on the x-axis ?

import plotly.express as px
import plotly.graph_objects as go

# Load the Iris dataset from Plotly Express
iris_data = px.data.iris()

# Create a new figure with a cumulative histogram 
fig = go.Figure(
    data=[go.Histogram(
        x=iris_data['sepal_width'], # Use sepal width as the variable
        cumulative_enabled=True # Enable cumulative mode
    )]
)

# Add labels and titles to the figure
fig.update_layout(
    title='Cumulative Histogram of Sepal Width in Iris Dataset',
    xaxis_title='Sepal Width',
    yaxis_title='Cumulative Frequency'
)

# Show the figure
fig.show()

How It Works

The code above performs the following steps:

  • Import the Plotly Express and Plotly Graph Objects libraries
  • Load the Iris dataset from Plotly Express into a variable called iris_data
  • Create a new figure with a cumulative histogram using the go.Figure method
  • Set the data for the histogram using the go.Histogram method, specifying the sepal_width column as the variable to plot
  • Enable cumulative mode by setting cumulative_enabled to True
  • Add labels and titles to the figure using the update_layout method
  • Display the resulting figure using the show method

Example 2: Horizontal Cumulative Histogram

A horizontal cumulative histogram displays the cumulative frequencies on the x-axis and the variable values on the y-axis ?

import plotly.express as px
import plotly.graph_objects as go

# Load the Iris dataset from Plotly Express
iris_data = px.data.iris()

# Create a new figure with a horizontal cumulative histogram
fig = go.Figure(
    data=[go.Histogram(
        y=iris_data['sepal_width'], # Use sepal width as the variable
        cumulative_enabled=True, # Enable cumulative mode
        orientation='h' # Set orientation to horizontal
    )]
)

# Add labels and titles to the figure
fig.update_layout(
    title='Horizontal Cumulative Histogram of Sepal Width in Iris Dataset',
    xaxis_title='Cumulative Frequency',
    yaxis_title='Sepal Width'
) 

# Show the figure
fig.show() 

Key Differences

The key differences between vertical and horizontal cumulative histograms:

  • Use y parameter instead of x for data input
  • Set orientation='h' to make the histogram horizontal
  • Swap the axis titles accordingly

Parameters

Parameter Description Values
cumulative_enabled Enables cumulative mode True/False
orientation Sets histogram orientation 'v' (vertical) / 'h' (horizontal)
x or y Data column to plot DataFrame column

Conclusion

Creating a cumulative histogram in Plotly is straightforward using the cumulative_enabled=True parameter. You can create both vertical and horizontal orientations by adjusting the data parameter and orientation setting. Plotly's interactive features make these visualizations excellent for exploring cumulative distributions in your data.

Updated on: 2026-03-27T01:31:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements