Modules & Packages · Unit 5

Matplotlib

Line, bar, histogram and pie charts

Unit 5 · Visualize data with Matplotlib

A chart can make a pattern easier to see

Matplotlib is a Python plotting library. Choose a chart, adjust the sample values, and inspect the matching Python code. The preview beside the editor is a browser-rendered teaching companion; the editable Python example runs Matplotlib inside Pyodide in your browser. The starter code saves its figure to the virtual file system so Matplotlib does not place a separate display below the lesson.

First run: Pyodide may need to download Matplotlib before the example runs. The shared runtime loads it only when a program imports it.

Choose and shape a chart

Use comparable positive values for this starter dataset. The preview responds to your edits.

Line chart preview

Browser preview

Python code for this chart

Run line chart code
Starting up Python…
import matplotlib.pyplot as plt
days = ["Mon","Tue","Wed","Thu","Fri"]
visitors = [12,18,15,22,17]
plt.plot(days, visitors, marker="o")
plt.title("Daily visitors")
plt.xlabel("Day")
plt.ylabel("Visitors")
plt.grid(True)
plt.savefig("chart.png")
print("Chart saved to chart.png")
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Read the plotting sequence

  1. 1 · Import

    import matplotlib.pyplot as plt gives pyplot a short alias, plt.

  2. 2 · Supply data

    Pass sequences of labels and numeric values.

  3. 3 · Add context

    A title and axis labels explain what the marks mean.

  4. 4 · Render

    plt.savefig() writes a PNG to the browser's virtual file system.

For a histogram, the bars count values in bins; for a bar chart, each category has a bar. Use a histogram for a numeric distribution and a bar chart for category comparisons.

Common plotting mistakes

Mismatched lengths

plt.plot(days, marks)

The x and y sequences must have compatible lengths.

Wrong chart for the question

Histogram for named categories

Histograms show numeric distributions; use bars for named categories.

Missing labels

A chart without title or axes

Add a concise title and axis labels so readers know what is measured.

Matplotlib import unavailable

First run in a fresh Pyodide session

The runtime fetches the package when first imported; allow it to finish loading.

Check your understanding

1. Which chart shows the distribution of numeric marks?

2. What does plt.xlabel() do?

3. Why use plt.grid(True)?