Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@DietBru

Description

@DietBru

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesNew feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions