-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
): | ||
""" | ||
|
@@ -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 | ||
------- | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚲 🏠 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐑 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 |
||
|
||
return figManager.canvas.figure | ||
|
||
|
||
|
There was a problem hiding this comment.
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.