How can I create a stacked bar chart in Pandas? 2

How can I create a stacked bar chart in Pandas?

Creating a stacked bar chart in Pandas involves using the Pandas library, a powerful tool for data analysis and manipulation in Python. This can be achieved by first importing the necessary libraries, then loading the data into a Pandas DataFrame. Next, the data can be grouped and aggregated using the appropriate functions. Finally, the stacked bar chart can be created using the Pandas plot function with the desired parameters. This allows for a visually appealing representation of data, showcasing the relationship between different variables and their respective values.

Create a Stacked Bar Chart in Pandas


You can use the following basic syntax to create a stacked bar chart in pandas:

df.groupby(['var1', 'var2']).size().unstack().plot(kind='bar', stacked=True)

The following example shows how to use this syntax in practice.

Example: Create Stacked Bar Chart in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'F', 'F', 'F'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4]})

#view DataFrame
print(df)

  team position  points
0    A        G       5
1    A        G       7
2    A        F       7
3    A        F       9
4    B        G      12
5    B        F       9
6    B        F       9
7    B        F       4

We can use the following code to create a stacked bar chart that displays the total count of position, grouped by team:

df.groupby(['team', 'position']).size().unstack().plot(kind='bar', stacked=True)

The x-axis shows the team name and the y-axis shows the total count of position for each team.

From the chart we can see that team A has 2 guards (G) and 2 forwards (F) while team B has 1 guard and 3 forwards.

We can also use the color and title arguments to modify the color of the bars and add a title to the chart:

df.groupby(['team', 'position']).size().unstack().plot(kind='bar', stacked=True,
            color=['steelblue','pink'], title='Position Count by Team')

stacked bar chart in pandas

A title has been added to the top of the plot and the colors of the bars have been changed to steelblue and pink, just as we specified.

The following tutorials explain how to create other common charts in Python:

Cite this article

stats writer (2024). How can I create a stacked bar chart in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-stacked-bar-chart-in-pandas/

stats writer. "How can I create a stacked bar chart in Pandas?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-stacked-bar-chart-in-pandas/.

stats writer. "How can I create a stacked bar chart in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-stacked-bar-chart-in-pandas/.

stats writer (2024) 'How can I create a stacked bar chart in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-stacked-bar-chart-in-pandas/.

[1] stats writer, "How can I create a stacked bar chart in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a stacked bar chart in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top