Python

# pyplot subplot()

The matplotlib.pyplot package is a series of methods that allow matplotlib to behave similarly to MATLAB. One of those functions is subplot(). The method subplot() creates a subplot to an existing image at the provided grid location. It is identical to a subplots() method, except instead of adding several subplots at once, it only inserts one at the moment. You will use the subplot() method in various code lines to make numerous plots. Another disadvantage of the subplot method is removing all previously created plots from your display. The subplots() method requires three inputs, specifying the figure’s arrangement. The present plot’s index is represented by the third parameter. Let’s do some examples of the subplot() method.

## Example 01:

Let’s get started with the very first example to see how a plot can be constructed in Spyder 3. You need to know that in python, you cannot create a plot without importing the matplotlib.pyplot package in the python tool to make our code executable for graphs. So, we have imported the matplotlib.pyplot in our tool as object “p”. After that, we have been importing the NumPy package as an “n” object to utilize and add support for using the NumPy arrays in our code. Now, we have to define the “x” and “y” axis for a plot. For this, we need to use the NumPy array with the NumPy object “n”. Both the arrays contain 4 elements of integer type and are saved to the respective axis. We have to use the pyplot libraries “plot()” function to create a plot with its axis. This function takes two more arguments, i.e. marker and color. The marker is used to show the intersection point of both axes, and the color is used to draw the line in red. Let’s save the code and execute it with the run button of Spyder.

import matplotlib.pyplot as p
import numpy as n
x = n.array([0, 1, 2, 3]) #x-axis
y = n.array([4, 9, 2, 11]) #y-axis
p.plot(x,y, marker="*", color="red") After running the code, we have got the below plot in the console. ## Example 02:

Now, let’s take a look at the subplot() function in python to create a subplot within the original plot. So, we have started our new example with matplotlib.pyplot package import as object “p”. After this, we have added the NumPy support using its package import with the “n” object. After this, we have added two axes of a plot using “x” and “y” and added values to both with the NumPy arrays separately. Now, the subplot() function of the pyplot package has been called with 2 rows, 1 column, and 1 subplot of the original plot. Now, the plot() function has been called. After this, we declared two axes, “x” and “y” using the NumPy array of 4 elements each. The second subplot has been created of 2 rows 1 column of the original plot using the subplot() function of pyplot. Now, the plot function has been called once again. The show() function is called with a pyplot “p” object to show the plots on the console.

import matplotlib.pyplot as p
import numpy as n
x = n.array([4, 9, 2, 11]) #x-axis
y = n.array([0, 1, 2, 3]) #y-axis
p.subplot(2, 1, 1)
p.plot(x,y)
x = n.array([13, 16, 9, 3]) #x-axis
y = n.array([2, 4, 6, 8]) #y-axis
p.subplot(2, 1, 2)
p.plot(x,y)
p.show() Let’s execute the code within Spyder 3. We have got the 2 subplots on the console screen, as shown below. These two plots have been created with 2 rows and 1 column only. ## Example 03:

Let’s have a look at our last and a little long example of the subplot() function in the python tool. So, we have started our new code with the same “matplotlib.pyplot” package support with its “p” object. The NumPy library object “n” has been added as well. Now, 6 subplots will be created in the Spyder 3 console. We have started with the declaration of x and y axes for the 6 different plots using the NumPy array function. All the arrays defined in the 1st, 3rd, and 5th axes are the same, while all the 2nd, 4th, and 6th axes are the same. The pyplot subplot() function has been utilized after every pair of x,y axes to make a subplot. This function has been taking 2 rows and 3 columns for each plot while the plot has been declared as 1, 2, 3, 4, 5, and 6. The plots for the 1st, 3rd, and 5th subplot() will be the same, while the plots for the 2nd, 4th, and 6th functions will be the same.

import matplotlib.pyplot as p
import numpy as n
x = n.array([0, 1, 2])
y = n.array([4, 8, 9])
p.subplot(2, 3, 1)
p.plot(x,y)
x = n.array([2, 4, 6])
y = n.array([3, 5, 7])
p.subplot(2, 3, 2)
p.plot(x,y)
x = n.array([0, 1, 2])
y = n.array([4, 8, 9])
p.subplot(2, 3, 3)
p.plot(x,y)
x = n.array([2, 4, 6])
y = n.array([3, 5, 7])
p.subplot(2, 3, 4)
p.plot(x,y)
x = n.array([0, 1, 2])
y = n.array([4, 8, 9])
p.subplot(2, 3, 5)
p.plot(x,y)
x = n.array([2, 4, 6])
y = n.array([3, 5, 7])
p.subplot(2, 3, 6)
p.plot(x,y)
p.show() After the execution, we have got the 1st, 3rd, and 5th as the same plot. While the 2nd, 4th, and 6th plot is the same as demonstrated below. ## Conclusion:

This was all about using the subplot() function of python in Spyder 3 to create plots as we do in MATLAB. We have tried to cover every necessary thing to make it easier. We are really confident that you will find it simple to understand and use. 