Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get a reverse-order cumulative histogram in Matplotlib?
To create a reverse-order cumulative histogram in Matplotlib, we use the parameter cumulative = -1 in the hist() method. This creates a histogram where each bin shows the cumulative count from the maximum value down to that bin, rather than from the minimum value up.
What is a Reverse Cumulative Histogram?
A reverse cumulative histogram displays the total count of values greater than or equal to each bin value. Instead of accumulating from left to right, it accumulates from right to left, showing how many data points exceed each threshold.
Basic Example
Let's create a simple reverse cumulative histogram ?
import matplotlib.pyplot as plt
# Sample data
data = [1, 2, 2, 3, 1, 4, 3, 0, 1, 3, 0]
# Create reverse cumulative histogram
plt.figure(figsize=(8, 5))
plt.hist(data, bins=5, edgecolor='black', alpha=0.7, cumulative=-1)
plt.title('Reverse Cumulative Histogram')
plt.xlabel('Values')
plt.ylabel('Cumulative Count (Reverse)')
plt.grid(True, alpha=0.3)
plt.show()
Comparing Normal vs Reverse Cumulative
Here's a side-by-side comparison to understand the difference ?
import matplotlib.pyplot as plt
data = [1, 2, 2, 3, 1, 4, 3, 0, 1, 3, 0]
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Normal cumulative histogram
ax1.hist(data, bins=5, edgecolor='black', alpha=0.7, cumulative=True)
ax1.set_title('Normal Cumulative Histogram')
ax1.set_xlabel('Values')
ax1.set_ylabel('Cumulative Count')
ax1.grid(True, alpha=0.3)
# Reverse cumulative histogram
ax2.hist(data, bins=5, edgecolor='black', alpha=0.7, cumulative=-1)
ax2.set_title('Reverse Cumulative Histogram')
ax2.set_xlabel('Values')
ax2.set_ylabel('Cumulative Count (Reverse)')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Parameters
| Parameter | Description | Example Values |
|---|---|---|
cumulative |
Direction of cumulation | True (forward), -1 (reverse) |
bins |
Number of histogram bins | 5, 10, 'auto' |
edgecolor |
Color of bin edges | 'black', 'white', 'red' |
Practical Use Case
Reverse cumulative histograms are useful for survival analysis or showing "how many exceed this value" ?
import matplotlib.pyplot as plt
import numpy as np
# Simulate test scores
np.random.seed(42)
scores = np.random.normal(75, 15, 100) # Mean 75, std 15
plt.figure(figsize=(10, 6))
n, bins, patches = plt.hist(scores, bins=15, edgecolor='black', alpha=0.7, cumulative=-1)
plt.title('Students Scoring Above Each Grade Level')
plt.xlabel('Score')
plt.ylabel('Number of Students')
plt.grid(True, alpha=0.3)
# Add percentage labels
total_students = len(scores)
for i, (bin_edge, count) in enumerate(zip(bins[:-1], n)):
if count > 0:
percentage = (count / total_students) * 100
plt.annotate(f'{count:.0f}\n({percentage:.1f}%)',
xy=(bin_edge, count),
xytext=(5, 5),
textcoords='offset points',
fontsize=8)
plt.show()
Conclusion
Use cumulative=-1 in plt.hist() to create reverse cumulative histograms. This visualization is particularly useful for showing "greater than or equal to" distributions and survival analysis scenarios.
