30

I'm kind of new in coding and thus in python so this may sound quite dumb, but what are the main differences between .subplot() and .subplots() methods from matplotlib in python?

I didn't find this explanation anywhere else and after reading the documentation from https://matplotlib.org/ I inferred that with both methods you can create as many figures and plots as you want...so for me both of them seem to be quite the same thing and they just differ the way you can handle plots, axes, etc...or am I wrong?

Btw, I am using python3 in jupyter notebook if it makes any difference.

2 Answers 2

27

1. matplotlib.pyplot.subplots()

From the documentation page on matplotlib.pyplot.subplots():

This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.

That means you can use this single function to create a figure with several subplots with only one line of code. For example, the code below will return both fig which is the figure object, and axes which is a 2x3 array of axes objects which allows you to easily access each subplot:

fig, axes = plt.subplots(nrows=2, ncols=3)

2. matplotlib.pyplot.subplot()

In contrast, matplotlib.pyplot.subplot() creates only a single subplot axes at a specified grid position. This means it will require several lines of code to achieve the same result as matplot.pyplot.subplots() did in a single line of code above:

# first you have to make the figure
fig = plt.figure(1)

# now you have to create each subplot individually
ax1 = plt.subplot(231)
ax2 = plt.subplot(232)
ax3 = plt.subplot(233)
ax4 = plt.subplot(234)
ax5 = plt.subplot(235)
ax6 = plt.subplot(236)

or you can also use built-in method of fig:

ax1 = fig.add_subplot(231)
ax2 = fig.add_subplot(232)
ax3 = fig.add_subplot(233)
ax4 = fig.add_subplot(234)
ax5 = fig.add_subplot(235)
ax6 = fig.add_subplot(236)

Conclusion

The code above can be condensed with a loop, but it is still considerably more tedious to use. I'd therefore recommend you use matplotlib.pyplot.subplots() since it is more concise and easy to use.

Sign up to request clarification or add additional context in comments.

2 Comments

The same applies to add_subplot() where you add a subplot each time. For ex: ax = fig.add_subplot(111) means just a single plot with grid 1x1. ax = fig.add_subplot(121) would mean 1st subplot in a 1x2 grid and ax = fig.add_subplot(122) would mean 2nd subplot in a 1x2 grid
@LZYan@Bazingaa, Thanks! :)
0

I have also racked my head on the difference between these two methods for years.

(1) The MATLAB style pyplot wrapper way, "subplots()" with an "s", seem more convenient for most users in a Jupyter notebook environment. There is less syntax to remember and Jupyter updates current figure each time a cell is run, so there is no confusion about which object is the current axes or current figure.

(2) The Object Oriented Method way, "subplot()" with NO "s", is more formal. It really shines when the code will be used in a scripting environment as part of a wider coding module. Since the code is not run interactively at run-time, the formal definition of current figure and axes objects are important for reducing bugs.

(3) Even in a Jupyter notebook environment, when the figure attributes are complex (for publication quality) or are created using python scripts (such as iterators), then the formalism of the Object Oriented Method helps reduce bugs and makes writing the scripts easier. Also, I have not found a way to create 3D projections (wire frame 3D graph) without using the Object Oriented Method. subplot3d = plt.subplot(111, projection='3d')

These are the main differences from my use cases.

While learning, Matplotlib documentation recommends picking one method and sticking with it. And in Python explicit is better than implicit, so the OO method is preferred. But I prefer a different recommendation from "Python Tools for Scientist" book. Mainly, it's better to learn both, because we need to read other people's code. So practice using one method for a while, then switch to the other method for the next while.

from: https://github.com/ageron/handson-ml3/blob/main/tools_matplotlib.ipynb

import matplotlib  
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

figure = plt.figure(1, figsize = (12, 4))
subplot3d = plt.subplot(111, projection='3d')
surface = subplot3d.plot_surface(X, Y, Z, rstride=1, cstride=1,
                                 cmap=matplotlib.cm.coolwarm, linewidth=0.1)
plt.show()

3d-projection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.