Creating a Bar Plot with Pandas and Matplotlib
2. Bar Plot with Pandas and Matplotlib
Write a Pandas program that create a Bar plot with Pandas and Matplotlib.
The following exercise create a bar plot using Pandas and Matplotlib to visualize categorical data.
Sample Solution :
Code :
import pandas as pd
import matplotlib.pyplot as plt
# Create a sample DataFrame
df = pd.DataFrame({
'Category': ['A', 'B', 'C', 'D'],
'Values': [50, 80, 30, 90]
})
# Create a bar plot of the 'Category' vs 'Values'
df.plot(kind='bar', x='Category', y='Values')
# Add a title
plt.title('Category vs Values')
# Display the plot
plt.show()
Output:

Explanation:
- Created a DataFrame with categories and corresponding values.
- Used the plot() function with kind='bar' to create a bar plot of 'Category' vs 'Values'.
- Added a title and displayed the plot using plt.show().
For more Practice: Solve these Related Problems:
- Write a Pandas program to create a vertical bar plot that shows the frequency of categorical data sorted in descending order.
- Write a Pandas program to generate a horizontal bar plot with custom colors for each bar and an accompanying legend.
- Write a Pandas program to create a stacked bar plot from a DataFrame with multiple categorical columns.
- Write a Pandas program to create a grouped bar plot from a pivoted DataFrame and include error bars representing standard deviation.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.