Matplotlib
Bar Chart
Draw vertical, horizontal, and grouped bars.
By EZ4Code Team
barchart
Code
import matplotlib.pyplot as plt
import numpy as np
categories = ["A", "B", "C", "D"]
values = [23, 45, 12, 67]
plt.bar(categories, values, color="steelblue", edgecolor="black")
plt.title("Sales by category")
plt.ylabel("units")
plt.show()
# Horizontal bars
plt.barh(categories, values, color="coral")
plt.xlabel("units")
plt.show()
# Grouped bars
x = np.arange(len(categories))
plt.bar(x - 0.2, values, 0.4, label="2023")
plt.bar(x + 0.2, [30, 40, 20, 50], 0.4, label="2024")
plt.xticks(x, categories)
plt.legend()
plt.show()Explanation
bar() and barh() render vertical or horizontal bars, accepting scalar or array colors and edge styling. Grouped bars use offset x positions to place multiple series side by side, with xticks restoring the category labels. Width and offsets control how bars are spaced.
More Matplotlib Snippets
Line Plot
Plot lines with markers, colors, and styles.
Scatter Plot
Visualize relationships with size and color encodings.
Subplots
Arrange multiple axes with subplots and GridSpec.
Labels and Legend
Annotate plots with text, arrows, and legend options.
Styles and Themes
Apply built-in styles and customize rcParams.
Save Figure
Export figures to PNG, PDF, and SVG with DPI control.