Matplotlib

Stacked bar graph in python : Step By Step

We can draw stacked bar graphs in python using matplotlib library python. We can create some dummy data and plot the same chart. For this dummy data creation, we can either use a NumPy array or we can provide it in Pandas data frame. Alternatively, we can use the seaborn library as well to achieve the same.

The stacked bar graph in python using Matplotlib –

Step 1: Importing & Dummy data creation

In this step, we will import the matplotlib package first, and then we will create the dummy data for visualization.

import matplotlib.pyplot as plt
#Dummy data 
x = ['Cat_1', 'Cat_2', 'Cat_3', 'Cat_4']
y1 = [16, 30, 38, 24]
y2 = [19, 35, 14, 35]

Step 2: Plotting stack barchart-

In order to plot the chart. Please follow the below syntax. We will first run the code and see the output and then follow the important notes.

plt.bar(x, y1, color='g')
plt.bar(x, y2, bottom=y1, color='b')
plt.show()

Here the most important thing is defining the second bottom chart. Here the y2 plot will take y1 as the bottom.

stacked barchart in python

 

Important Alternatives –

1. plotting multiple bar graphs in python –

Firstly, I will say there is no difference between the above section only we need to create one data point. Also, we need to adjust the bottom parameter. Let’s see the below example and see –

import matplotlib.pyplot as plt
import numpy as np
#Dummy data 
x = ['Cat_1', 'Cat_2', 'Cat_3', 'Cat_4']
y1 = np.array([16, 30, 38, 24])
y2 = np.array([19, 35, 14, 35])
y3 = np.array([8, 12, 14, 10])
plt.bar(x, y1, color='g')
plt.bar(x, y2, bottom=y1, color='b')
plt.bar(x, y3, bottom=y1+y2, color='y')
plt.show()

The difference between the above section and this one is the data structure for underline data. In the above section, it was in a list format and for the multibar chart,  It is in the NumPy chart. Here is the output of matplotlib stacked bar chart code.

plotting multiple bar graphs in python

 

2. Pandas as a data source for stack barchart-

Please run the below code. Here we are using pandas dataframe and converting it to stacked bar chart.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data_frame = pd.DataFrame([['Cat_1', 16, 30, 38, 24], ['Cat_2', 19, 35, 14, 35], ['Cat_3', 8, 12, 14, 10],
                   ['Cat_4', 10, 18, 11, 19]],
                  columns=['X-Axis', 'A', 'B', 'C', 'D'])
data_frame.plot(x='X-Axis', kind='bar', stacked=True, title='Stacked Bar with dataframe')
stacked bar chart using pandas

3. Stacked Bar chart using Seaborn Library –

Let’s take a dummy dataframe and in one step, we will draw the stacked bar chart using the seaborn library.

import pandas
import matplotlib.pylab as plt
import seaborn as sns
df = pandas.DataFrame(dict(
   X_Axis=[2, 5, 1, 6, 3],
   y1=[56, 21, 34, 36, 12],
   y2=[29, 13, 17, 21, 8]
))
bar_plot1 = sns.barplot(x='X_Axis', y='y1', data=df, label="Y1", color="b")
bar_plot2 = sns.barplot(x='X_Axis', y='y2', data=df, label="Y2", color="g")
plt.show()
stacked barchart using seaborn

Thanks 

Data Science Learner Team