You have 2 free member-only stories left this month.

What Are the “plt” and “ax” in Matplotlib Exactly?

Plotting on a paper, or in a cell on the paper?

Indeed, as the most popular and fundamental data visualisation library, Matplotlib is kind of confusing in some perspectives. It is usually to see that someone asking about

  • When should I use “axes”?
  • Why some examples using “plt” while someone else using “ax”?
  • What’s the difference between them?

It is good that there are so many examples online to show people how to use Matplotlib to draw this kind of chart or that kind of chart, but I rarely see any tutorials mentioning “why”. This may cause people who have less programming experience or switching from other languages like R becomes very confusing.

In this article, I won’t teach you to draw any specific charts using Matplotlib but will try to explain the basic but important regarding Matplotlib — what are the “plt” and “ax” people usually use.

Concepts

Photo by AbsolutVision on Pixabay

To clarify, when I say “plt”, it doesn’t exist in the Matplotlib library. It is called “plt” because most of Python programmers like to import Matplotlib and make an alias called “plt”, which I believe you should know, but just in case.

import matplotlib.pyplot as plt

Then, come back to our main topic. Let’s draw a simple chart for demonstration purposes.

import numpy as npplt.plot(np.random.rand(20))
plt.title('test title')
plt.show()

As shown in the above-annotated screenshot, when we draw a graph using plt:

  1. A Figure object is generated (shown in green)
  2. An Axes object is generated implicitly with the plotted line chart (shown in red)
  3. All the elements of the plot such as x and y-axis are rendered inside the Axes object (shown in blue)

Well, if we use some kind of metaphor here:

  • Figure is like a paper that you can draw anything you want
  • We have to draw a chart in a “cell”, which is Axes in this context
  • If we’re drawing only one graph, we don’t have to draw a “cell” first, just simply draw on the paper anyway. So, we can use plt.plot(...).

Explicitly Draw the “Cell”

Photo by LUM3N on Pixabay

Of course, we can explicitly draw a “cell” on the “paper”, to tell Matplotlib that we’re gonna draw a chart inside this cell. Then, we have the following code.

fig, ax = plt.subplots()
ax.plot(np.random.rand(20))
ax.set_title('test title')
plt.show()

Exactly the same results. The only difference is that we explicitly draw the “cell” so that we are able to get the Figure and Axes object.

Indeed, when we just want to plot one graph, it is not necessary to “draw” this cell. However, you must be noticed that we have to do this when we want to draw multiple graphs in one plot. In other words, the subplots.

n_rows = 2
n_cols = 2
fig, axes = plt.subplots(n_rows, n_cols)
for row_num in range(n_rows):
for col_num in range(n_cols):
ax = axes[row_num][col_num]
ax.plot(np.random.rand(20))
ax.set_title(f'Plot ({row_num+1}, {col_num+1})')
fig.suptitle('Main title')
fig.tight_layout()
plt.show()

In this code snippet, we firstly declared how many rows and columns we want to “draw”. 2 by 2 means that we want to draw 4 “cells”.

Then, in each cell, we plot a random line chart and assign a title based on its row number and column number. Please note that we’re using Axes instances.

After that, we define a “Main title” on the “paper”, which is the Figure instance. So, we have this supertitle that does not belong to any “cell”, but on the paper.

Finally, before calling the show() method, we need to ask the “paper” — Figure instance — to automatically give enough padding between the cells by calling its tight_layout() method. Otherwise,

Summary

Photo by bodobe on Pixabay

Hopefully, now you understand better what are plt and ax people are using exactly.

Basically, the plt is a common alias of matplotlib.pyplot used by most people. When we plot something using plt such as plt.line(...), we implicitly created a Figure instance and an Axes inside the Figure object. This is totally fine and very convenient when we just want to draw a single graph.

However, we can explicitly call plt.subplots() to get the Figure object and Axes object, in order to do more things on them. When we want to draw multiple subplots on a Figure, it is usually required to use this approach.

Also, here are the Matplotlib official API reference for the Figure and Axes classes. It is highly recommended to check them out and try some methods yourselves to make sure you understand even deeper.

Machine Learning Engineer@AEMO | Cloud Solution Architect | Data Engineer | Data Scientist | PhD of Computer Science

Sign up for The Variable

By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge research to original features you don't want to miss. Take a look.

You'll need to sign in or create an account to receive this newsletter.

Your home for data science. A Medium publication sharing concepts, ideas and codes.

submission.csv? No thanks!

This is the continuation of my previous article:

From Jupyter Notebook To Scripts

Last time we discussed how to convert Jupyter Notebook to scripts, together with all sorts of basic engineering practices such as CI, unit testing, package environment, configuration, logging, etc

Even with the script form, it still requires us to change the configuration and run the script, It is OK for Kaggle competitions because all you need is just the submission.csv, but you probably don’t want to sit behind the computer 24/7 and hit Run whenever users send you a prediction request 🙁

In this article, we will…


Photo by Paweł Czerwiński at unsplash

Deciding what to test is the first, and most important, step in defining an AI prototype. This decision shapes all other decisions in designing the prototype.

Defining the hypothesis under test is important because prototypes are messy. And messy experiments give muddled results; hiding the relevant amongst the incidental.

Prototypes are broad brush-stroked approximations of the final product. The learnings from a prototype can be game-changing, intriguing, and wholly surprising. But to learn from a prototype with confidence, the effect or insight will need to be large.

It is very easy to take a finding from a prototype and generalise…


Photo by Estée Janssens on Unsplash

And how to implement it in your forecasting model using Gradient Boosting regression

According to Wikipedia, feature engineering refers to the process of using domain knowledge to extract features from raw data via data mining techniques. These features can then be used to improve the performance of machine learning algorithms.

Feature engineering does not necessarily have to be fancy though. One simple, yet prevalent, use case of feature engineering is in time-series data. The importance of feature engineering in this realm is due to the fact that (raw) time-series data usually only contains one single column to represent the time attribute, namely date-time (or timestamp).

Regarding this date-time data, feature engineering can be…


Enhanced data exploration that goes beyond descriptives

Save time, resources and stay healthy with data exploration that goes beyond means, distributions and correlations: Leverage PCA to see through the surface of variables. It saves time and resources, because it uncovers data issues before an hour-long model training and is good for a programmer’s health, since she trades off data worries with something more enjoyable. For example, a well-proven machine learning model might fail, because of one-dimensional data with insufficient variance or other related issues. PCA offers valuable insights that make you confident about data properties and its hidden dimensions.

This article shows how to leverage PCA to…


Providing a simple 3-step template to upskill and transition to python

Photo by Christina @ wocintechchat.com on Unsplash

Many companies are running common data analytics tasks using python scripts. They are asking employees to convert scripts that may currently exist in SAS or other toolsets to python. One step of this process is being able to pull in the same data with the new techniques. This article is about converting DB2 queries into python scripts.

How do you convert your queries to python? It may sound overwhelming but it’s easier than you think. Once you have a template for your data source, all you need to do is change the query and output filename.

There are several ways…