Skip to content
dreamcode
dreamcode
Map
Data plotting
Lesson 76 of 77
+15 XP on finish
PYTHON APPLIEDChapter 13 · Python for Data Science

Visualizing data with Matplotlib

Matplotlib is Python's foundational plotting library. Its pyplot interface lets you create line plots, bar charts, scatter plots, and histograms with just a few function calls.

Worked example
import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr", "May"]
temps = [2, 5, 11, 16, 21]

plt.figure(figsize=(8, 4))
plt.plot(months, temps, marker="o", color="#7b68ee")
plt.title("Average Temperature")
plt.ylabel("Celsius")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("temps.png")
plt.show()

How it reads

  • plt.plot(x, y) draws a line chart connecting the data points
  • marker='o' adds circular markers at each data point
  • plt.savefig('temps.png') saves the chart to an image file
Cloud tip: For quick DataFrame plots, use df.plot() directly. It calls Matplotlib under the hood but saves you from manual axis setup.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. Which function creates a line chart in Matplotlib?
  2. 2. How do you save a Matplotlib chart to a file?
  3. 3. Which chart type is best for comparing category counts?
Answer every question to unlock the next lesson.