Why is a plot in Matplotlib figures called axis?

It’s a bit confusing because I associate “axis” with cartesian geometry

Do you mean the class Axes?

It is plural. I suppose it is like calling something, that is a collection, by the plural name of its constituents.

Like calling a forest “the trees”.

That’s actually the right intuition. An Axes represents a paricular “x axis and y axis” that you can plot on. A single Figure can have multiple axes (aka “subplots”).

2 Likes

So, a subplot is also referred to as an axis simply because it is displayed in two dimensions within the figure, and the type of subplot is irrelevant? For example, the plot might be of any sort, such as a pie type of plot, and it’d still be called an axis, even though there are no axes involved?

To be clear, it’s called an Axes (plural), not an axis. And a pie chart still has axes even if they aren’t visible–it’s plotted in polar coordinates.

In matplotlib, pie charts aren’t Axes themselves, they are a thing you can plot on an Axes.

Roughly, an Axes is a 2D (or 3D) space on which you can display “Artists”. Artists are things you see on the plot, and include lines, patches, text, etc.

Taking the pie chart demo as an example,

>>> import matplotlib.pyplot as plt

>>> labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
>>> sizes = [15, 30, 45, 10]

>>> fig, ax = plt.subplots()
>>> ax.pie(sizes, labels=labels)
([<matplotlib.patches.Wedge at 0x7f1e796da4a0>,
  <matplotlib.patches.Wedge at 0x7f1e796db7c0>,
  <matplotlib.patches.Wedge at 0x7f1e796dbca0>,
  <matplotlib.patches.Wedge at 0x7f1e797201c0>],
 [Text(0.9801071672559598, 0.4993895680663527, 'Frogs'),
  Text(-0.33991877217145816, 1.046162142464278, 'Hogs'),
  Text(-0.49938947630209474, -0.9801072140121813, 'Dogs'),
  Text(1.0461621822461364, -0.3399186497354948, 'Logs')])

This bit of code creates a Figure with one Axes (fig, ax = plt.subplots()). To that Axes, it adds 8 Artists to create a pie chart: 4 “Wedge” artists (the slices on the pie chart), and 4 “Text” artists (the labels). The Artist objects are what gets returned by ax.pie.

2 Likes

Thanks, I went back to my book to double-check because that’s where I thought I had seen a subplot being referred to as an ‘axis,’ but I couldn’t find it. So I thought I might have hallucinated, but then I looked back at my chat with ChatGPT and realized that was my source.

Specifically, it said:
‘In Matplotlib, a figure is the entire plotting area or canvas, and an axis (often referred to as “ax”) is a single subplot within that figure. The subplots() function is used to create both the figure and axes.’

I guess it was ChatGPT that hallucinated? Or maybe not? Maybe a subplot can be called both ways, axis and axes?

The answer is almost always that ChatGPT hallucinated, or it was imitating someone else who made this mistake. ChatGPT is not a reliable source for programming details.

3 Likes

Thanks, man. That was clear. The axes are just the empty cells/ spaces, right?

Yep, thank you guys for your help

Axes is the plural of axis, and it keeps that meaning in matplotlib. An Axes has individual Axis, which model the actual x-axis and y-axis in a plot. You can access them with ax.xaxis/ax.yaxis.

Basically - its just the container that holds all the plot information.

Just because I didn’t see this posted, they actually have a nice breakdown of the terms in their quickstart guide: Quick start guide — Matplotlib 3.8.2 documentation

2 Likes