88

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

3 Answers 3

165

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

1
  • 2
    Many thanks for the answer! And how to get rid of the empty space in between the subplots now, and let two plots fill all the window width? tight_layout() doesn't work properly for me... Commented Jul 12, 2023 at 12:52
33
ax.set_visible(False)

will suffice in most cases.

0
6

Remove the axis from the figure doc:

axs[1].remove()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.