Bar charts compare categorical values by drawing one rectangle for each category. In Matplotlib, they fit counts, totals, rankings, and other side-by-side measurements where the category label matters as much as the numeric value.
The object-oriented interface keeps the Figure and Axes explicit. plt.subplots() creates the drawing area, ax.bar() draws the rectangles, ax.bar_label() writes value labels on the bars, and fig.savefig() exports the finished chart.
Categorical labels can be passed directly to ax.bar() when each label is unique. Saving a PNG and printing the stored title, axis labels, bar count, filename, and byte size checks the chart without relying on an interactive window.
Related: How to create a line chart in Matplotlib
Related: How to set plot colors in Matplotlib
Related: How to set axis labels in Matplotlib
Steps to create a Matplotlib bar chart:
- Save the bar chart script as create_bar_chart.py.
- create_bar_chart.py
from pathlib import Path import matplotlib.pyplot as plt plans = ["Standard", "Pro", "Enterprise", "Education"] signups = [180, 245, 90, 135] colors = ["tab:blue", "tab:orange", "tab:green", "tab:red"] fig, ax = plt.subplots(layout="constrained") bars = ax.bar(plans, signups, color=colors) ax.bar_label(bars, padding=3) ax.set_title("Quarterly signups by plan") ax.set_xlabel("Plan") ax.set_ylabel("Signups") ax.set_ylim(0, max(signups) + 60) output = Path("plan-signups-bar-chart.png") fig.savefig(output, dpi=160) plt.close(fig) print(f"bars: {len(bars)}") print(f"title: {ax.get_title()}") print(f"x label: {ax.get_xlabel()}") print(f"y label: {ax.get_ylabel()}") print(f"saved: {output}") print(f"bytes: {output.stat().st_size}")
ax.bar() accepts the plan names as categorical x values, and ax.bar_label() places each count above its bar.
- Run the script from the Python environment that has Matplotlib installed.
$ python create_bar_chart.py bars: 4 title: Quarterly signups by plan x label: Plan y label: Signups saved: plan-signups-bar-chart.png bytes: 33385
The byte count can change with Matplotlib, font, backend, or DPI differences. A nonzero byte count and the expected filename confirm that the PNG was written.
- Open plan-signups-bar-chart.png and confirm the plotted result.
The image should show four colored bars labeled Standard, Pro, Enterprise, and Education, with the highest bar for Pro.
- Replace the plan labels, values, and colors with the categories for the real dataset.
plans = ["North", "South", "East", "West"] signups = [120, 98, 143, 111] colors = ["tab:blue", "tab:blue", "tab:blue", "tab:blue"]
Keep the category and value sequences the same length because each label needs one numeric bar height.
- Remove the practice files when the chart was only a test.
$ rm create_bar_chart.py plan-signups-bar-chart.png
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.