bhylu.com

Advertisements

Matplotlib Get Started

Start plotting in a local virtual environment with Matplotlib installed. A small saved figure verifies the package and output location without requiring a graphical window.

Understand the idea

Use py -m pip install matplotlib in the selected environment. The example writes workshop.png in the current folder. This website shows a pre-rendered result below, while the text-only Python playground does not provide an interactive plotting window. Change the code locally or in a suitable notebook.

Where to try it

Run this complete script in a local virtual environment with Matplotlib installed (py -m pip install matplotlib). It uses a non-interactive backend and saves workshop.png in the current folder. The site shows a rendered example below; its text-only online runner does not display plot windows.

A worked example

Python

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")
ax.bar(["Morning", "Evening"], [12, 18])
ax.set(ylabel="Attendees", title="First workshop chart")
fig.savefig("workshop.png", dpi=140)
plt.close(fig)
print("Saved workshop.png")

Read the result

Two bars compare 12 morning attendees with 18 evening attendees.

Two bars compare 12 morning attendees with 18 evening attendees.
Rendered from this original example. Change and rerun the script locally to make your own chart.

Try one change

Run the script in your project environment and locate the generated PNG.

For package options, see the Matplotlib documentation.