Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 57 ยท Getting Started

What Is Matplotlib?

Meet Matplotlib, Python's foundational plotting library. Learn what it does, how a figure becomes pixels, how it fits with NumPy, pandas and seaborn.

Matplotlib is the library that draws charts for Python. You give it numbers, and it gives you a line plot, a bar chart, a heatmap, a 3D surface or a hand-built infographic, as an image or as a vector file. It has been around for two decades, and almost every other Python plotting tool is either built on top of it or designed to look different from it. If you work with data in Python, you will meet it sooner or later.

This tutorial uses Matplotlib 3.10, the version that runs in your browser on this site. Every example is live: press Run and the figure appears right under the code. You do not need to install anything. If you have never plotted anything, the quick taste in Python Matplotlib and the image examples in Arrays as Images and Plots are good warm-ups, but this course starts from zero and does not require them.

What Matplotlib is good at

Matplotlib is a general-purpose, low-level plotting library. "Low-level" does not mean hard. It means you control every part of the picture: the size of the canvas, the tick marks, the font of each label, the thickness of every line. That control is why it is the right tool for:

  • publication-quality figures for papers, reports and slides;
  • custom chart types that no ready-made library offers;
  • plots embedded in scripts, servers and automated reports (no window needed);
  • understanding what higher-level libraries do under the hood.

The price of that control is that a polished chart takes a few more lines than in a point-and-click tool. The lessons ahead teach the patterns that keep those lines short.

What you can draw

Lines Trends, functions, time series.
Bars Compare categories side by side.
Heatmaps Grids of numbers as color.
3D and polar Surfaces, radar charts, roses.

One library, one consistent way of working.

1 / 2

From numbers to pixels

Every Matplotlib picture goes through the same pipeline. Understanding it once explains most of the library's behavior later.

flowchart LR
    A["Your data<br/>lists, arrays, Series"] --> B["Figure and Axes<br/>(objects in memory)"]
    B --> C["Artists<br/>lines, text, patches"]
    C --> D["Renderer<br/>Agg, SVG, PDF"]
    D --> E["Output<br/>image on screen or file"]

Your code builds objects first; a renderer turns them into pixels or vector shapes only at the end.

Your data goes into artists: a line artist, a text artist, a rectangle artist. Artists live inside an Axes (the rectangle with the plot), and the Axes lives inside a Figure (the whole canvas). Nothing is drawn until a renderer is asked to produce output. The renderer decides the format: Agg makes pixel images such as PNG, while the SVG and PDF renderers write scalable shapes. The next lesson, Your First Plot, builds a chart step by step, and Anatomy of a Figure names every part.

Python Check the version and the pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printed types show the hierarchy: a Figure contains an Axes, and the Axes contains a Line2D. The backend prints as Agg, the pixel renderer: here the runner draws into an image instead of opening a window, which is also what servers and scripts without a screen do.

How charts appear on this site

On your own computer, plt.show() normally opens a window or draws inside a notebook cell. In the in-browser runner on this site, a non-interactive image backend is selected, and each figure that is shown (or still open when the program ends) is converted to a PNG and displayed below the code, in the order it was created. Two practical consequences:

  • There are no hover tooltips or zoom buttons here. Interactivity and Widgets shows how those work on a desktop, with simulated events for the site.
  • Code that writes files to disk cannot do so here, so the saving lessons write into memory buffers instead.

Everything else, including the code you copy, behaves the same on your machine.

Matplotlib and its neighbors

Matplotlib rarely works alone.

NumPy supplies the arrays. A Matplotlib line is two arrays of numbers (x and y), and NumPy is how you make them: np.linspace, np.sin, random samples. See the NumPy tutorial if arrays are new.

pandas includes plotting shortcuts: df.plot() creates a Matplotlib figure for you, and you can pass it an ax= to place the chart on one you built. Because they share the same objects, you can start in pandas and finish in Matplotlib. Plotting NumPy and pandas Data covers it.

seaborn is a statistical layer on top of Matplotlib: fewer lines for distributions, regressions and category comparisons, with attractive defaults. It is not available in this in-browser runner, so Seaborn-Style Recipes in Pure Matplotlib re-creates its best-known charts by hand, which doubles as a lesson in what seaborn does for you.

Plotly, Bokeh and Altair make interactive web charts. They are different projects with their own models, compared in The Matplotlib Ecosystem.

Python A first look: NumPy data, Matplotlib picture

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not worry about every call yet. Notice the rhythm: create a figure and an Axes, add data, add labels, show. That rhythm repeats in nearly every chart in this tutorial.

Matplotlib has two ways of being called, a quick state-based style (plt.plot(...)) and an explicit object style (ax.plot(...)). You will see both for a while; pyplot vs the Object-Oriented API explains when to use which. Most of this tutorial uses the object style because it scales to bigger figures.

What you will learn

The track moves in ten chapters: getting started, line styling, core chart types (bars, scatter, histograms, heatmaps), multi-panel layouts, annotation and polish, working with data, 3D and animation, saving and production, six projects, and practice with interview questions. Each lesson ends with a short quiz.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Matplotlib mainly do?

    Matplotlib is a plotting library: it turns numbers into figures such as lines, bars, heatmaps and 3D surfaces.
  2. 2. Which object is the whole canvas that contains the plot rectangles?

    The Figure is the top-level canvas. It contains one or more Axes, and each Axes contains the artists such as lines and text.
  3. 3. When is a Matplotlib figure actually turned into pixels or vector shapes?

    Calls like ax.plot only create artist objects in memory. A renderer draws them when you show or save the figure.
  4. 4. How does seaborn relate to Matplotlib?

    seaborn creates Matplotlib figures and axes behind the scenes, so you can customize its output with ordinary Matplotlib calls.

Summary

  • Matplotlib is Python's foundational plotting library, giving fine control over every part of a chart.
  • Data becomes artists inside an Axes inside a Figure; a renderer turns them into an image or vector file at the end.
  • NumPy supplies arrays, pandas has plot shortcuts that return Matplotlib objects, and seaborn is a statistical layer on top.
  • On this site, figures are drawn by an image backend and appear below the code; no window or file is needed.
  • The rhythm is always: make a Figure and Axes, add data, add labels, show.

Next, draw a chart yourself in Your First Plot.