0
0
Pandasdata~5 mins

Bar plots in Pandas

Choose your learning style9 modes available
Introduction

Bar plots help us see and compare values easily using bars. They make numbers simple to understand by showing them as tall or short bars.

Comparing sales numbers of different products in a store.
Showing the number of students in different classes.
Visualizing survey results with categories like favorite colors.
Comparing monthly expenses in a budget.
Syntax
Pandas
DataFrame.plot.bar(x=None, y=None, **kwargs)

x is the column for categories (labels on x-axis).

y is the column for values (height of bars).

Examples
Basic bar plot using 'Category' for labels and 'Value' for bar heights.
Pandas
df.plot.bar(x='Category', y='Value')
Bar plot using DataFrame index as x-axis and all numeric columns as bars.
Pandas
df.plot.bar()
Horizontal bar plot instead of vertical bars.
Pandas
df.plot.barh(x='Category', y='Value')
Sample Program

This code creates a bar plot showing sales numbers for three fruits. Each bar height shows how many were sold.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
sales_data = {'Product': ['Apples', 'Bananas', 'Cherries'], 'Sales': [30, 45, 25]}
df = pd.DataFrame(sales_data)

# Create bar plot
ax = df.plot.bar(x='Product', y='Sales', color='skyblue', legend=False)
ax.set_ylabel('Number Sold')
ax.set_title('Sales of Fruits')
plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

You need matplotlib installed to see the plot.

Use legend=False if you want to hide the legend for a single bar series.

Customize colors and labels to make the plot clearer.

Summary

Bar plots show data as bars for easy comparison.

Use plot.bar() with x and y to pick labels and values.

Customize your plot with colors, titles, and labels for clarity.