The following is the procedure for making a stacked plot with N-series using Matplotlib and Pandas:
Import necessary packages: import pandas as pd
and import matplotlib.pyplot as plt
Read in the data as a Pandas dataframe: df = pd.read_csv('data.csv')
Group the data by category: grouped = df.groupby('category')
Calculate the sum of each category for each year: stacked_data = grouped.sum()
Transpose the data to make it easier to plot: stacked_data = stacked_data.T
Create a stacked plot: stacked_data.plot(kind='bar', stacked=True)
Add axes labels and a title: plt.xlabel('Year')
, plt.ylabel('Value')
, plt.title('Stacked Plot')
Show the plot: plt.show()
Asked: 2023-05-15 02:29:51 +0000
Seen: 9 times
Last updated: May 15 '23