the full guide

Matplotlib: drawing charts

Line, scatter, bar and histogram charts, with labels. Press Run to see each chart.

15 min Beginner

What you need: The earlier pages of this course. Nothing to install.

Line and scatter

plot_line.py
import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
score = [40, 55, 60, 72, 85]

plt.plot(days, score, marker="o", label="Practice score")
plt.scatter([2, 4], [70, 50], color="red", label="Mock tests")
plt.xlabel("Day")
plt.ylabel("Score")
plt.title("Learning over a week")
plt.legend()
plt.grid(True)
plt.show()
print("Chart drawn")

Output

Chart drawn

Bars and histograms

plot_bars.py
import matplotlib.pyplot as plt
import numpy as np

fig, (left, right) = plt.subplots(1, 2, figsize=(9, 3.5))

left.bar(["Maths", "Science", "English"], [78, 85, 90])
left.set_title("Marks by subject")

heights = np.random.default_rng(1).normal(160, 8, 500)
right.hist(heights, bins=20)
right.set_title("Heights of 500 students")

plt.show()
print("Charts drawn")

Output

Charts drawn
ChartUse it to show
LineChange over time
ScatterHow two values relate
BarComparing categories
HistogramHow values are spread

You're ready for machine learning

You now know enough Python for every lesson in the Machine Learning module. Good next steps: the spam detector, then the KNN guide.

Finished reading?