Delete a subplot

Question:

I’m trying to figure out a way of deleting (dynamically) subplots in matplotlib. I see they have a remove method, but I get the error

NotImplementedError: cannot remove artist

I’m surprised that I can’t find this anywhere. Does anyone know how to do this?

from matplotlib import pyplot as plt

fig, axs = plt.subplots(1,3)

axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])

plt.draw()
plt.tight_layout()

enter image description here

Asked By: Jeff

||

Answers:

Use fig.delaxes or plt.delaxes to remove unwanted subplots

fig, axs = plt.subplots(1,3)
axs[0].plot([1,2],[3,4])
axs[2].plot([0,1],[2,3])

fig.delaxes(axs[1])

plt.draw()
plt.tight_layout()

enter image description here

Answered By: Jeff
ax.set_visible(False)

will suffice in most cases.

Answered By: naught101

Remove the axis from the figure doc:

axs[1].remove()
Answered By: stansy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.