Python Pandas - Plot a Stacked Horizontal Bar Chart


For a stacked Horizontal Bar Chart, create a Bar Chart using the barh() and set the parameter “stacked” as True

Stacked = True

At first, import the required libraries −

import pandas as pd
import matplotlib.pyplot as plt

Create a DataFrame with 3 columns −

dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
})

Plotting stacked Horizontal Bar Chart with all the columns −

dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan"))

Example

Following is the complete code −

import pandas as pd
import matplotlib.pyplot as plt

# creating dataframe
dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
})

# plotting stacked Horizontal Bar Chart with all the columns
dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan"))

# display the plotted Horizontal Bar Chart
plt.show()

Output

This will produce the following output −

Updated on: 01-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements