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()
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-15 02:29:51 +0000
Seen: 10 times
Last updated: May 15 '23