Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5737ce4

Browse filesBrowse files
authored
Merge pull request #7023 from vollbier/figure_clf_kwarg
Add `clf` kwarg to plt.figure()
2 parents 0ce413f + e249ac3 commit 5737ce4
Copy full SHA for 5737ce4

File tree

Expand file treeCollapse file tree

3 files changed

+65
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+65
-2
lines changed
Open diff view settings
Collapse file
+32Lines changed: 32 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
New parameter `clear` for :func:`~matplotlib.pyplot.figure`
2+
-----------------------------------------------------------
3+
4+
When the pyplot's function :func:`~matplotlib.pyplot.figure` is called
5+
with a ``num`` parameter, a new window is only created if no existing
6+
window with the same value exists. A new bool parameter `clear` was
7+
added for explicitly clearing its existing contents. This is particularly
8+
useful when utilized in interactive sessions. Since
9+
:func:`~matplotlib.pyplot.subplots` also accepts keyword arguments
10+
from :func:`~matplotlib.pyplot.figure`, it can also be used there::
11+
12+
import matplotlib.pyplot as plt
13+
14+
fig0 = plt.figure(num=1)
15+
fig0.suptitle("A fancy plot")
16+
print("fig0.texts: ", [t.get_text() for t in fig0.texts])
17+
18+
fig1 = plt.figure(num=1, clear=False) # do not clear contents of window
19+
fig1.text(0.5, 0.5, "Really fancy!")
20+
print("fig0 is fig1: ", fig0 is fig1)
21+
print("fig1.texts: ", [t.get_text() for t in fig1.texts])
22+
23+
fig2, ax2 = plt.subplots(2, 1, num=1, clear=True) # clear contents
24+
print("fig0 is fig2: ", fig0 is fig2)
25+
print("fig2.texts: ", [t.get_text() for t in fig2.texts])
26+
27+
# The output:
28+
# fig0.texts: ['A fancy plot']
29+
# fig0 is fig1: True
30+
# fig1.texts: ['A fancy plot', 'Really fancy!']
31+
# fig0 is fig2: True
32+
# fig2.texts: []
Collapse file

‎lib/matplotlib/pyplot.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
431431
edgecolor=None, # defaults to rc figure.edgecolor
432432
frameon=True,
433433
FigureClass=Figure,
434+
clear=False,
434435
**kwargs
435436
):
436437
"""
@@ -457,10 +458,19 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
457458
resolution of the figure. If not provided, defaults to rc figure.dpi.
458459
459460
facecolor :
460-
the background color. If not provided, defaults to rc figure.facecolor
461+
the background color. If not provided, defaults to rc figure.facecolor.
461462
462463
edgecolor :
463-
the border color. If not provided, defaults to rc figure.edgecolor
464+
the border color. If not provided, defaults to rc figure.edgecolor.
465+
466+
frameon : bool, optional, default: True
467+
If False, suppress drawing the figure frame.
468+
469+
FigureClass : class derived from matplotlib.figure.Figure
470+
Optionally use a custom Figure instance.
471+
472+
clear : bool, optional, default: False
473+
If True and the figure already exists, then it is cleared.
464474
465475
Returns
466476
-------
@@ -558,6 +568,9 @@ def make_active(event):
558568
if _INSTALL_FIG_OBSERVER:
559569
fig.stale_callback = _auto_draw_if_interactive
560570

571+
if clear:
572+
figManager.canvas.figure.clear()
573+
561574
return figManager.canvas.figure
562575

563576

Collapse file

‎lib/matplotlib/tests/test_figure.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ def test_fignum_exists():
4949
assert_equal(plt.fignum_exists(4), False)
5050

5151

52+
@cleanup
53+
def test_clf_keyword():
54+
# test if existing figure is cleared with figure() and subplots()
55+
fig0 = plt.figure(num=1)
56+
fig0.suptitle("A fancy plot")
57+
assert_equal([t.get_text() for t in fig0.texts], ["A fancy plot"])
58+
59+
fig1 = plt.figure(num=1, clear=False)
60+
fig1.text(0.5, 0.5, "Really fancy!")
61+
assert_true(fig0 is fig1)
62+
assert_equal([t.get_text() for t in fig1.texts],
63+
["A fancy plot", 'Really fancy!'])
64+
65+
fig2, ax2 = plt.subplots(2, 1, num=1, clear=True)
66+
assert_true(fig0 is fig2)
67+
assert_equal([t.get_text() for t in fig2.texts], [])
68+
69+
5270
@image_comparison(baseline_images=['figure_today'])
5371
def test_figure():
5472
# named figure support

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.