Bar Charts with bar(x, height)ΒΆ

A bar chart displays categorical or discrete data as rectangular bars, where the length of each bar is proportional to the value it represents. The Axes.bar() method takes x-positions and corresponding heights, and draws vertical bars centered at each x position.

Why this matters for data science: Bar charts are the most common visualization for comparing quantities across categories – think of model accuracy comparisons, feature importance rankings, or frequency counts from value_counts(). The width parameter controls how wide each bar is (here set to 1 to make bars touch), while edgecolor="white" with a small linewidth creates clean visual separation between adjacent bars. The x-positions are offset by 0.5 using np.arange(8) so that bars are centered between the tick marks. Understanding how to control bar width, spacing, and edge styling is essential for creating publication-quality comparative visualizations.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# make data:
x = 0.5 + np.arange(8)
y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0]

# plot
fig, ax = plt.subplots()

ax.bar(x, y, width=1, edgecolor="white", linewidth=0.7)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()