Area plots represent the structure and development of objects over time or by category, usually in the form of a rectangle or square divided into different domains.

Creating a 100% stacked area plot in Matplotlib involves normalizing your data so that the sum of each category at any given point in time equals 1 (or 100 if you're working with percentages directly).

This normalization ensures that the total height of the stacked areas always reaches the 100% mark.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# 1. Prepare your data
# Example data: three groups showing their evolution over time
data = pd.DataFrame({
    'group_A': [1, 4, 6, 8, 9],
    'group_B': [2, 24, 7, 10, 12],
    'group_C': [2, 8, 5, 10, 6],
}, index=range(1, 6))

# 2. Normalize the data to percentages (or fractions)
# Divide each value by the sum of its row (across all groups)
data_perc = data.divide(data.sum(axis=1), axis=0)

# 3. Create the 100% stacked area plot
plt.stackplot(
    data_perc.index,  # X-axis values (e.g., time points)
    data_perc["group_A"],
    data_perc["group_B"],
    data_perc["group_C"],
    labels=['Group A', 'Group B', 'Group C']
)

# 4. Customize the plot
plt.legend(loc='upper left')  # Add a legend
plt.margins(0, 0)  # Remove extra margins
plt.title('100% Stacked Area Chart')
plt.xlabel('Time')
plt.ylabel('Percentage Contribution')
plt.yticks(np.arange(0, 1.1, 0.1), [f'{int(i*100)}%' for i in np.arange(0, 1.1, 0.1)]) # Format y-axis as percentages

# 5. Display the plot
plt.show()

Output:

Figure_1.png