Ask Your Question
0

What is the method for producing a stacked n-series plot using Pandas and Matplotlib?

asked 2023-05-02 01:59:37 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-02 02:15:02 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-02 01:59:37 +0000

Seen: 9 times

Last updated: May 02 '23