Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The following is the procedure for making a stacked plot with N-series using Matplotlib and Pandas:

  1. Import necessary packages: import pandas as pd and import matplotlib.pyplot as plt

  2. Read in the data as a Pandas dataframe: df = pd.read_csv('data.csv')

  3. Group the data by category: grouped = df.groupby('category')

  4. Calculate the sum of each category for each year: stacked_data = grouped.sum()

  5. Transpose the data to make it easier to plot: stacked_data = stacked_data.T

  6. Create a stacked plot: stacked_data.plot(kind='bar', stacked=True)

  7. Add axes labels and a title: plt.xlabel('Year'), plt.ylabel('Value'), plt.title('Stacked Plot')

  8. Show the plot: plt.show()