w3resource

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:

Pandas - Creating a Bar Plot with Pandas and Matplotlib

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/pandas/create-a-bar-plot-using-pandas-and-matplotlib.php