Creating a Bar Plot with Pandas and Matplotlib
Pandas: Visualization Integration Exercise-2 with Solution
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().
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics