Python Matplotlib
A chart often says more than a table of numbers. matplotlib is Python's standard library for charts: line charts, bar charts, pie charts, histograms, scatter plots and many more. You build a chart with a few calls on matplotlib.pyplot, usually imported as plt, and plt.show() displays it. In our editor the chart appears in a graphics window over the code, and savefig saves it as a picture in your project.
A line chart
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12, 18, 15, 22, 28, 25]
plt.plot(months, sales, marker="o", color="teal", label="2026")
plt.title("Sales per month")
plt.xlabel("Month")
plt.ylabel("Units sold")
plt.grid(True, alpha=0.3)
plt.legend()
plt.show()
print("Best month:", months[sales.index(max(sales))])Best month: May
plot draws a line through the points. title, xlabel and ylabel add text, grid adds light guide lines, and legend explains the lines using their label. Call plot several times before show to draw several lines in one chart.
Bar and pie charts
import matplotlib.pyplot as plt
languages = ["Python", "C", "C++", "Java"]
students = [45, 25, 18, 12]
plt.figure(figsize=(6.4, 3.2))
plt.bar(languages, students, color=["#1e3a8a", "#2563eb", "#60a5fa", "#93c5fd"])
plt.title("Favourite language")
plt.ylabel("Students")
plt.show()
plt.pie(students, labels=languages, autopct="%1.0f%%")
plt.title("Share of students")
plt.show()bar draws one bar per category, and pie shows parts of a whole, with autopct printing each percentage. figure(figsize=(width, height)) sets the size in inches; at the default 100 dots per inch, 6.4 by 3.2 inches is 640 by 320 pixels.
Several charts in one figure
plt.subplots(rows, cols) creates a figure with a grid of charts, called axes. You then draw on each axes object:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig, (left, right) = plt.subplots(1, 2, figsize=(8, 3))
left.plot(x, np.sin(x), label="sin")
left.plot(x, np.cos(x), label="cos")
left.legend()
left.set_title("Waves")
rng = np.random.default_rng(1)
right.scatter(rng.normal(size=50), rng.normal(size=50), alpha=0.6)
right.set_title("Random points")
fig.tight_layout()
fig.savefig("charts.png")
plt.show()
print("saved charts.png")saved charts.png
With axes objects, the methods start with set_: set_title, set_xlabel, set_ylim. tight_layout spaces the charts so that their labels do not overlap, and savefig writes the figure as a PNG file that you can download from the file list.
Charts from data
matplotlib works with lists, NumPy arrays and pandas columns alike. A pandas DataFrame can even draw itself: df.plot(x="month", y="amount", kind="bar") makes a bar chart from two columns.
Practice exercises
- Draw a line chart of the temperatures of a week, with a title and labelled axes.
- Draw a bar chart of the marks of five students, with the bars of students below 40 in red.
- Draw a pie chart of how you spend the 24 hours of a day.
- Draw
x ** 2andx ** 3for x from -3 to 3 in two charts side by side, and save them as a PNG.