Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing a subplot in Matplotlib

I have a number of subplots in a figure fig1, created via

ax = fig1.add_subplot(221) 

I then plot stuff in each of the subplots via

im=ax.plot(x,y) 

and add some axis labels via

ax.set_xlabel('xlabel') 

I would then like to clear a specific subplot completely, as described in When to use cla(), clf() or close() for clearing a plot in matplotlib?. However the problem is that ax.cla()and ax.clear() seem to only clear the data from the plot, without removing the axes, axis tick labels etc. On the other hand plt.clf() clears the entire figure. Is there something in between? A clf-like command that clears everything in a subplot, including axis labels? Or have I simply used the commands in a wrong way?

like image 441
Wolpertinger Avatar asked Nov 14 '17 10:11

Wolpertinger


People also ask

How to clear a plot in Matplotlib?

Here are the syntax and parameters of the Matplotlib clear plot function. The axis.clear () function accepts no parameters. The ‘keep_observers’ parameter in figure.clear () function is a boolean value. The clear () function does not return any value. It is used in python programs to clear plots.

How to clear the data from a subplot?

you can use subplot.cla() # which clears data but not axes subplot.clf() # which clears data and axes Share Improve this answer Follow answered Feb 2, 2017 at 20:45

How to control the default spacing of the subplots in Matplotlib?

This module is used to control the default spacing of the subplots and top level container for all plot elements. The clear () method figure module of matplotlib library is used to Clear the figure. keep_observers: This parameter is the boolean value.

What is Matplotlib in Python?

Matplotlib is a library in Python, which is a numerical – mathematical extension for NumPy library. The figure module of the Matplotlib library provides the top-level Artist, the Figure, which contains all the plot elements. The figure module is used to control the subplots’ default spacing and top-level container for all plot elements.


1 Answers

  • ax.clear() clears the axes. That is, it removes all settings and data from the axes such that you are left with an axes, just as it had been just created.

  • ax.axis("off") turns the axes off, such that all axes spines and ticklabels are hidden.

  • ax.set_visible(False) turns the complete axes invisible, including the data that is in it.

  • ax.remove() removes the axes from the figure.

Complete example:

import matplotlib.pyplot as plt  fig,axes = plt.subplots(2,3) for ax in axes.flat:     ax.plot([2,3,1])  axes[0,1].clear() axes[1,0].axis("off") axes[1,1].set_visible(False) axes[0,2].remove()  plt.show() 

enter image description here

like image 133
ImportanceOfBeingErnest Avatar answered Oct 07 '22 09:10

ImportanceOfBeingErnest