Data Visualization with Matplotlib

Matplotlib is Python's most established plotting library. With a handful of pyplot calls you can turn raw numbers into line charts, scatter plots, bar charts, and histograms — and the object-oriented API gives you full control over every figure and axes.

Learn Data Visualization with Matplotlib in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

⚠️ Requires pip install matplotlib and a local Python display

Different data calls for different chart types:

Annotate plots with the label= argument plus legend() :

Create explicit Figure and Axes objects with plt.subplots() :

This is the recommended style for anything beyond a quick one-off chart.

axes is an array you index into; each entry is its own Axes you draw on.

Call savefig before show() — showing can clear the current figure in some backends.

✔ Draw line, scatter, bar, and histogram charts

Reach for the fig, ax API whenever a plot grows beyond a quick sketch.

📋 Quick Reference — Matplotlib

You can now turn data into clear, styled charts and export them for reports.

Up next: FastAPI — build fast, typed web APIs in Python.

Practice quiz

Which Matplotlib module provides the familiar plot, scatter, and bar functions?

  • matplotlib.pyplot
  • matplotlib.axes
  • matplotlib.style
  • matplotlib.figure

Answer: matplotlib.pyplot. matplotlib.pyplot (conventionally imported as plt) is the high-level plotting interface.

What is the conventional import for pyplot?

  • import pyplot as plt
  • from matplotlib import plot as plt
  • import matplotlib.pyplot as plt
  • import matplotlib as plt

Answer: import matplotlib.pyplot as plt. The community convention is 'import matplotlib.pyplot as plt'.

Which function draws a line chart connecting x,y points?

  • plt.scatter(x, y)
  • plt.plot(x, y)
  • plt.bar(x, y)
  • plt.hist(x)

Answer: plt.plot(x, y). plt.plot draws a line connecting the points; scatter draws unconnected markers.

Which function is best for showing the distribution of a single dataset?

  • plt.bar
  • plt.plot
  • plt.scatter
  • plt.hist

Answer: plt.hist. plt.hist bins values and draws a histogram, ideal for visualizing how data is distributed.

What does fig, ax = plt.subplots() return?

  • A figure and an axes (or array of axes)
  • Two figures
  • An axes and a legend
  • Two axes only

Answer: A figure and an axes (or array of axes). plt.subplots() returns a Figure and one or more Axes objects, the basis of the object-oriented API.

In the object-oriented API, how do you set the title on an axes named ax?

  • plt.set_title('T')
  • ax.set_title('T')
  • ax.title('T')
  • fig.title('T')

Answer: ax.set_title('T'). On an Axes object you call ax.set_title(); the pyplot equivalent is plt.title().

Which call makes labels passed via the label= argument appear on the plot?

  • plt.show()
  • plt.grid()
  • plt.label()
  • plt.legend()

Answer: plt.legend(). plt.legend() (or ax.legend()) collects the label= values from plotted artists into a legend box.

How do you save a figure to a PNG file?

  • fig.download('chart.png')
  • plt.export('chart.png')
  • plt.savefig('chart.png')
  • plt.write('chart.png')

Answer: plt.savefig('chart.png'). plt.savefig('chart.png') (or fig.savefig) writes the current figure to disk; the format follows the extension.

What is the difference between the pyplot interface and the object-oriented API?

  • They produce different file formats
  • pyplot uses an implicit 'current' figure/axes state machine, while the OO API uses explicit fig/ax objects
  • pyplot is faster at rendering
  • Only the OO API can draw bars

Answer: pyplot uses an implicit 'current' figure/axes state machine, while the OO API uses explicit fig/ax objects. pyplot tracks an implicit current figure/axes; the OO API gives you explicit Figure and Axes objects, preferred for complex or multi-panel plots.

Which argument to plt.subplots creates a 2-by-2 grid of axes?

  • nrows=2, ncols=2
  • shape=4
  • grid=(2, 2)
  • rows=2, cols=2 only

Answer: nrows=2, ncols=2. plt.subplots(nrows=2, ncols=2) returns the figure and a 2x2 array of Axes you can index.