#255 Percentage stacked area chart

#255 Percent stacked area chart





A percentage stacked area chart is very close from a classic stacked area chart. However, values are normalised to make in sort that the sum of each group is 100 at each position on the X axis.

In this example we consider 3 groups, displayed in a pandas data frame. The first step is to normalise the data. Then it is possible to make the plot using the common stackplot function.






# library
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Make data
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))

# We need to transform the data from raw data to percentage (fraction)
data_perc = data.divide(data.sum(axis=1), axis=0)

# Make the plot
plt.stackplot(range(1,6),  data_perc["group_A"],  data_perc["group_B"],  data_perc["group_C"], labels=['A','B','C'])
plt.legend(loc='upper left')
plt.title('100 % stacked area chart')

Leave a Reply

Your email address will not be published.