Skip to content

Add clf kwarg to plt.figure() #7023

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

Merged
merged 2 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/users/whats_new/figure_new_clear_keyword.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
New parameter `clear` for :func:`~matplotlib.pyplot.figure`
-----------------------------------------------------------

When the pyplot's function :func:`~matplotlib.pyplot.figure` is called
with a ``num`` parameter, a new window is only created if no existing
window with the same value exists. A new bool parameter `clear` was
added for explicitly clearing its existing contents. This is particularly
useful when utilized in interactive sessions. Since
:func:`~matplotlib.pyplot.subplots` also accepts keyword arguments
from :func:`~matplotlib.pyplot.figure`, it can also be used there::

import matplotlib.pyplot as plt

fig0 = plt.figure(num=1)
fig0.suptitle("A fancy plot")
print("fig0.texts: ", [t.get_text() for t in fig0.texts])

fig1 = plt.figure(num=1, clear=False) # do not clear contents of window
fig1.text(0.5, 0.5, "Really fancy!")
print("fig0 is fig1: ", fig0 is fig1)
print("fig1.texts: ", [t.get_text() for t in fig1.texts])

fig2, ax2 = plt.subplots(2, 1, num=1, clear=True) # clear contents
print("fig0 is fig2: ", fig0 is fig2)
print("fig2.texts: ", [t.get_text() for t in fig2.texts])

# The output:
# fig0.texts: ['A fancy plot']
# fig0 is fig1: True
# fig1.texts: ['A fancy plot', 'Really fancy!']
# fig0 is fig2: True
# fig2.texts: []
17 changes: 15 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
edgecolor=None, # defaults to rc figure.edgecolor
frameon=True,
FigureClass=Figure,
clear=False,
**kwargs
):
"""
Expand All @@ -457,10 +458,19 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
resolution of the figure. If not provided, defaults to rc figure.dpi.

facecolor :
the background color. If not provided, defaults to rc figure.facecolor
the background color. If not provided, defaults to rc figure.facecolor.

edgecolor :
the border color. If not provided, defaults to rc figure.edgecolor
the border color. If not provided, defaults to rc figure.edgecolor.

frameon : bool, optional, default: True
If False, suppress drawing the figure frame.

FigureClass : class derived from matplotlib.figure.Figure
Optionally use a custom Figure instance.

clear : bool, optional, default: False
If True and the figure already exists, then it is cleared.

Returns
-------
Expand Down Expand Up @@ -558,6 +568,9 @@ def make_active(event):
if _INSTALL_FIG_OBSERVER:
fig.stale_callback = _auto_draw_if_interactive

if clear:
figManager.canvas.figure.clear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without committing myself yet as to whether the PR as a whole is a good idea, I think that the clearing should be unconditional, not based on whether there are axes objects. Otherwise, it is only a partial clearing of the figure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚲 🏠 fig is a ref to the current figure that we already have just above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fig ref only exists if in Line 520 if figManager is None.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐑

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄


return figManager.canvas.figure


Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ def test_fignum_exists():
assert_equal(plt.fignum_exists(4), False)


@cleanup
def test_clf_keyword():
# test if existing figure is cleared with figure() and subplots()
fig0 = plt.figure(num=1)
fig0.suptitle("A fancy plot")
assert_equal([t.get_text() for t in fig0.texts], ["A fancy plot"])

fig1 = plt.figure(num=1, clear=False)
fig1.text(0.5, 0.5, "Really fancy!")
assert_true(fig0 is fig1)
assert_equal([t.get_text() for t in fig1.texts],
["A fancy plot", 'Really fancy!'])

fig2, ax2 = plt.subplots(2, 1, num=1, clear=True)
assert_true(fig0 is fig2)
assert_equal([t.get_text() for t in fig2.texts], [])


@image_comparison(baseline_images=['figure_today'])
def test_figure():
# named figure support
Expand Down