Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plt.subplots() does not remove existing subplots when called on existing figure #6285

Closed
DietBru opened this issue Apr 10, 2016 · 3 comments
Closed
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature
Milestone

Comments

@DietBru
Copy link
Contributor

DietBru commented Apr 10, 2016

When using plt.subplots(..., num=101) with explicit figure number num-parameter,
the figure window does not get cleared before the new subplots are created. This can be seen by running the following code fragment:

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0, 5, 200)

fg1, axx = plt.subplots(2, 2, num=101)  # first call to create subplots
for i, ax in enumerate(axx.ravel(), start=1):  # fill subplots:
    ax.set_title(r"$x(t) = %i\cdot\sin(%i\cdot 2\pi t)$" % (i, i))
    ax.plot(t, i*np.sin(i*2*np.pi*t))

# Emulate second call, when rerunning script in interactive session:
fg1, axx = plt.subplots(2, 2, num=101)  # This places 4 new subplots above the old ones

fg1.canvas.draw()
plt.show()

The second plt.subplots()-call produces 4 new empty subplots above the old ones. I am not sure if this is the intended behavior, but I think removing existing subplots first would be more intuitive. Alternatively, adding a boolean clf=False keyword to the plt.figure() function would be even more useful and would not change the default behavior.

Thanks.

PS: This issue came up, when doing interactive data exploration in Sypder/IPython with the following work flow:

  1. Run script, place all figures (often more than 5) nicely across monitor
  2. Inspect figures and modify script
  3. Rerun script & goto 2.

For this use case the subplots() command is quite unsuitable in my opinion, because the figure needs to be for explicitly cleared first.

PPS: Tested on Debian/Sid with Python 3.5.1 and Matplotlib 1.5.1-1+b2

@tacaswell tacaswell added this to the unassigned milestone Apr 11, 2016
@tacaswell tacaswell modified the milestones: 2.1 (next point release), unassigned Apr 11, 2016
@anntzer
Copy link
Contributor

anntzer commented Aug 26, 2016

This issue was discussed here: #5146 (comment) and #5139 (comment) and we decided against it.

@tacaswell tacaswell added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues and removed API: consistency labels Aug 27, 2016
@tacaswell
Copy link
Member

@vollbier Would you be interested in opening a PR to add the clf kwarg?

I suggest adding an explicit fig.clf() to your scripts or [ax.cla() for ax in axx]

@anntzer
Copy link
Contributor

anntzer commented Sep 2, 2016

FWIW I really don't like that, looks like API bloat to me :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature
Projects
None yet
Development

No branches or pull requests

3 participants