Fundamentals 6 min read

Creating Custom Bar Charts with Python Matplotlib

This tutorial demonstrates how to use Python's Matplotlib library to generate single and multi‑bar charts, customize axis labels, adjust font size and rotation, and resolve common display issues by loading styles and Chinese fonts, providing complete code examples for reproducible visualizations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Custom Bar Charts with Python Matplotlib

The article explains how to quickly draw bar charts that meet specific requirements using Python.

Loading libraries

import matplotlib.pyplot as plt import matplotlib.font_manager as mfm from matplotlib import style style.use('ggplot') # load ggplot style font_path = "/System/Library/Fonts/STHeiti Light.ttc" # local Chinese font prop = mfm.FontProperties(fname=font_path)

Single bar chart

Data is defined as a list total = [3, 5, 6, 7, 8, 6, 4, 3, 3, 22, 4, 8, 7, 13, 7, 7, 15, 10, 6, 52, 8, 2, 3, 26, 1, 1, 0, 2, 0, 3] . The basic plot is created with:

plt.bar(range(len(total)), total) plt.title('单一柱图', fontproperties=prop) plt.savefig("单一柱图.png", dpi=700, fontproperties=prop) plt.show()

The initial result shows incorrect axis scaling and missing last bar, so the article improves the chart by customizing the x‑axis.

Custom x‑axis labels

Labels are generated for intervals of 10, showing only every 50th label and the first interval as "0~10":

x_labels = [] for item in range(0, 300, 10): x = item + 10 if x == 10: x_labels.append("{}~{}".format(0, 10)) elif x % 50 == 0: x_labels.append(str(x)) else: x_labels.append(None) x = range(len(total)) plt.bar(x, total) plt.title('单一柱图', fontproperties=prop) plt.xticks(x, x_labels) plt.savefig("单一柱图.png", dpi=700, fontproperties=prop) plt.show()

Font size and rotation can be further adjusted:

plt.xticks(x, x_labels, rotation=30, fontsize=5)

Multi‑bar chart

Data for three groups is stored in a 2‑D list:

A = [[28, 3, 5, 6, 3, 7, 2, 4, 9, 95], [58, 6, 13, 13, 5, 12, 17, 11, 10, 102], [60, 22, 21, 41, 11, 5, 1, 2, 3, 4]]

Horizontal positions and labels are generated similarly, then three bars are plotted side by side with different colors and a legend:

# generate x‑axis labels x_labels = [] for item in range(0, 100, 10): x = item + 10 if x == 10: x_labels.append("{}~{}".format(0, 10)) else: x_labels.append(str(x)) x = np.arange(10) plt.bar(x + 0.00, A[0], color='orange', width=0.3, label='A') plt.bar(x + 0.30, A[1], color='royalblue', width=0.3, label='B') plt.bar(x + 0.60, A[2], color='brown', width=0.3, label='C') plt.title('多柱图', fontproperties=prop) plt.xticks(x + 0.30, x_labels) plt.legend(loc='best') plt.savefig('多柱图.png', dpi=700, fontproperties=prop) plt.show()

The article concludes with the resulting charts and encourages readers to explore further Python learning resources.

Pythondata-visualizationMatplotlibPlottingBar Chart
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.