Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - Plot a Grouped Horizontal Bar Chart will all the columns
A grouped horizontal bar chart displays multiple data series side by side horizontally. In Pandas, you can create this using the barh() method without specifying x and y parameters, which automatically uses all numeric columns.
Setting Up the Data
First, import the required libraries and create a DataFrame with multiple numeric columns ?
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame with car specifications
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]
})
print(dataFrame)
Car Cubic_Capacity Reg_Price
0 Bentley 2000 7000
1 Lexus 1800 1500
2 BMW 1500 5000
3 Mustang 2500 8000
4 Mercedes 2200 9000
5 Jaguar 3000 6000
Creating the Grouped Horizontal Bar Chart
Set the "Car" column as the index and use barh() to create the chart. When no x and y parameters are specified, all numeric columns are plotted ?
import pandas as pd
import matplotlib.pyplot as plt
# Create 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]
})
# Set Car as index for proper labeling
dataFrame.set_index('Car', inplace=True)
# Create grouped horizontal bar chart
dataFrame.plot.barh(title='Car Specifications', color=("blue", "orange"))
plt.xlabel('Values')
plt.ylabel('Car Models')
plt.legend(title='Metrics')
plt.tight_layout()
plt.show()
Customizing the Chart
You can customize colors, add labels, and improve the layout for better visualization ?
import pandas as pd
import matplotlib.pyplot as plt
# Create and prepare data
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]
})
dataFrame.set_index('Car', inplace=True)
# Create customized horizontal bar chart
ax = dataFrame.plot.barh(
title='Car Specifications Comparison',
color=['skyblue', 'lightcoral'],
figsize=(10, 6)
)
# Add customizations
ax.set_xlabel('Values')
ax.set_ylabel('Car Models')
ax.legend(title='Specifications', loc='lower right')
ax.grid(axis='x', alpha=0.3)
plt.tight_layout()
plt.show()
Key Points
| Parameter | Purpose | Example |
|---|---|---|
title |
Chart title | 'Car Specifications' |
color |
Bar colors | ('blue', 'orange') |
figsize |
Chart dimensions | (10, 6) |
set_index() |
Row labels | Use categorical column |
Conclusion
Use DataFrame.plot.barh() without specifying x and y parameters to automatically plot all numeric columns as a grouped horizontal bar chart. Set a categorical column as the index for proper row labeling.
