Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method for producing a stacked n-series plot using Pandas and Matplotlib is:

  1. Create a Pandas DataFrame with the data to be plotted.
  2. Use the plot method of the DataFrame, setting kind='bar' and stacked=True.
  3. Customize the plot using Matplotlib functions and setters.

Example code:

import pandas as pd
import matplotlib.pyplot as plt

# create sample data
data = {'A': [10, 20, 30],
        'B': [5, 15, 25],
        'C': [15, 10, 5]}
df = pd.DataFrame(data)

# plot stacked bar chart
ax = df.plot(kind='bar', stacked=True)

# add labels and legend
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('Title')
ax.legend(title='Legend')

# show plot
plt.show()

This will produce a stacked bar chart with three series (A, B, C) and three categories on the X-axis. You can customize the colors, labels, and other properties of the plot by using Matplotlib functions on the returned AxesSubplot object.