Figures, plots & subplots: A simple cheatsheet for plotting graphs & images in Python
As a former electrical engineering student, my ‘go-to’ language has always been Matlab. Matlab is great for numerical analysis (including implementing deep learning models with recent updates); however, Matlab is not free.
During my undergraduate studies, I learnt Python. Python is one of the most popular programming languages, especially in the field of data science; it has many built-in functions and modules to facilitate data analysis.
In fact, my ‘go-to’ language has recently been shifting to Python. I am falling in love with Python. Python is absolutely great, but I always struggle with making figures using matplotlib
— which is one of the most commonly used Python modules for plotting figures, graphs, charts, etc.
For me (or perhaps many people), the confusing part of matplotlib
is that the following four statements are not particularly intuitive:
- plot:
matplotlib.pyplot
- subplot:
matplotlib.pyplot.subplots
- fig:
matplotlib.figure.Figure
- axis:
matplotlib.axes.Axes
This blog post will be my future ‘cheat sheet’ for matplotlib
.
Simple plot
Let’s start with a simple example. We have numerical input (x
) and output (y
) values. We can plot this with plt.plot()
and save the plot with plt.savefig()
.
Subplots
A single subplot
Let’s take a look at matplotlib.subplots
. Note, we are going to save the figure with fig.savefig()
, whereas we used fig.savefig()
on the previous example in order to save the plot.
Subplots (Define the number of subplots first)
This time, we are going to create multiple plots on a single figure. We define the number of plots at the beginning.
Subplots (Not define the number of plots first, but add plots one-by-one)
We are able to create a figure without defining the number of plots a priori. In this case, we need to define the location of plots.
Subplots (Same size, but change the dots per inch)
You may notice in the figure above that the axes and ticks are too small to read. We are able to change the font size, but also the dpi. The size of the figure is figsize
×dpi
. The previous figure size was 800×400, which is because figsize
×dpi
= (20, 10)×40 = (800, 400). The figure size of this following example is also 800×400, but figsize
=(4, 2) and dpi
=200. In other words, (4, 2)×200 = (800, 400).
If you add another line, plt.tight_layout()
, the layout becomes much neat.
Plot image files
We have been playing around with subplots for a while. Finally, let’s try to plot images. In Python, there are multiple ways to open image files, for example:
matplotlib mpimg
Pillow Image
OpenCV cv2
There are some nuances with the syntax for each module, and we must be especially careful when we open image files via cv2
and plot them. On top of this, being a computer vision enthusiast, I use PyTorch often. The following example covers how to tensorise and de-tensorise image files.
Summary
Key ideas are 1) we are able to make a plot via subplots
, and 2) we are able to make a figure comprising of multiple plots via subplots
.
Hope this article helps you as much as it has helped me!