0
0
Matplotlibdata~5 mins

Vertical bar chart with plt.bar in Matplotlib

Choose your learning style9 modes available
Introduction

A vertical bar chart helps you compare different groups using bars that go up and down. It makes numbers easy to see and understand.

Comparing sales numbers for different products in a store.
Showing the number of students in different classes.
Visualizing monthly expenses to see which month costs more.
Comparing votes for different candidates in an election.
Syntax
Matplotlib
plt.bar(x, height, width=0.8, color=None, edgecolor=None, label=None)

# x: positions or categories on x-axis
# height: heights of the bars (values)
# width: thickness of bars (optional)
# color: fill color of bars (optional)
# edgecolor: border color of bars (optional)
# label: label for legend (optional)

The x can be numbers or category names.

The height is how tall each bar will be.

Examples
Simple vertical bars at positions 1, 2, 3 with heights 5, 7, and 3.
Matplotlib
plt.bar([1, 2, 3], [5, 7, 3])
Bars labeled A, B, C with blue color.
Matplotlib
plt.bar(['A', 'B', 'C'], [10, 15, 7], color='skyblue')
Narrow bars with black edges.
Matplotlib
plt.bar(['X', 'Y'], [4, 9], width=0.5, edgecolor='black')
Sample Program

This code draws a vertical bar chart showing how many of each fruit we have. The bars are orange with brown edges. The chart has a title and labels for clarity.

Matplotlib
import matplotlib.pyplot as plt

# Categories and their values
fruits = ['Apple', 'Banana', 'Cherry', 'Date']
quantities = [10, 15, 7, 12]

# Create vertical bar chart
plt.bar(fruits, quantities, color='orange', edgecolor='brown')

# Add title and labels
plt.title('Fruit Quantities')
plt.xlabel('Fruit')
plt.ylabel('Quantity')

# Show the plot
plt.show()
OutputSuccess
Important Notes

Always label your axes to make the chart easy to understand.

You can change bar colors to make the chart more attractive or clear.

If you use category names for x, matplotlib places bars evenly spaced.

Summary

Vertical bar charts show values as bars going up from the x-axis.

Use plt.bar() with categories and values to create them.

Customize colors, width, and labels to make charts clear and nice.