How to delete a subplot in Matplotlib Python

In this tutorial, we are going to see how to delete a subplot in matplotlib – Python. Firstly, let us see what a matplotlib package is. It is a plotting library in Python which is used to create graphs and charts for the given data, this is widely in various sectors for various operations. Most of the segments of matplotlib are written in python and some segments are written in C and JavaScript.

A subplot is a group of axes that can exist in a single matplotlib figure. Python has an inbuilt function called subplot() which divides a single figure into multiple panes which are numbered row-wise. The subplot() function takes three arguments that are rows, columns and index where index value is not mandatory.

Generating subplots:

from matplotlib import pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
x1 = np.array([0, 1, 2, 3])
y1 = np.array([10, 20, 30, 40])
fig, axs = plt.subplots(2,3)
axs[0][0].plot(x,y)
axs[1][2].plot(x1,y1)
plt.show()

Output:

How to delete a subplot in Matplotlib Python

Explanation:

  • Import the matplotlib package and Numpy package to create arrays.
  • Assign values for X coordinate and Y coordinate by passing the values in an array.
  • Use subplot() function to display the number of plots in the display.
  • Plot the graph using plot() function.
  • Use show() function to display the graph.

Delete a subplot in matplotlib

from matplotlib import pyplot as plt
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
x1 = np.array([0, 1, 2, 3])
y1 = np.array([10, 20, 30, 40])
fig, axes = plt.subplots(2,3)
axes[0][0].plot(x,y)
axes[1][2].plot(x1,y1)
fig.delaxes(axes[0][0])
plt.show()

Output:

 

How to delete a subplot in Matplotlib Python

Explanation:

  • Import the matplotlib package and Numpy package to create arrays.
  • Assign values for the X coordinate and Y coordinate by passing the values in an array.
  • Use the subplot() function to display the number of plots in the display.
  • Access a specific subplot using the index where the first index represents the row value and the second index represents the column value.
  • delete a specific subplot by specifying the index.  Here the first subplot is passed as a parameter and so it is deleted. (fig.delaxes function)
  • Plot the graph using the plot() function.
  • Use show() function to display the graph.

NOTE:

By changing the index different subplots can be deleted for example in the above example when delaxes() is given as

fig.delaxes(axes[0][2])

the output will be different. which is given below

Subplot4

Leave a Reply

Your email address will not be published.