-
-
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
Conversation
@@ -557,6 +564,9 @@ def make_active(event): | |||
if _INSTALL_FIG_OBSERVER: | |||
fig.stale_callback = _auto_draw_if_interactive | |||
|
|||
if clf and len(figManager.canvas.figure.axes) > 0: | |||
figManager.canvas.figure.clear() |
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.
I thought the suggestion was to put this flag on |
There is a potentially reasonable justification for it only in |
Note that as of 2.1 you can already write
Actually, thinking about it again, it's not even clear why you'd want to reuse a figure. Can't you just close whatever figure you don't care about and create a new one? |
On 2016/09/06 7:06 PM, Antony Lee wrote:
No, that's not at all the point. Suppose you want to cycle through |
On 2016/09/06 7:06 PM, Antony Lee wrote:
Yes, but with mpl one can also use a name in place of the number; and |
Good point, didn't think about this use. |
I actually use the |
@@ -429,6 +429,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N | |||
facecolor=None, # defaults to rc figure.facecolor | |||
edgecolor=None, # defaults to rc figure.edgecolor | |||
frameon=True, | |||
clear=False, |
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.
This needs to be the last arg to maintain back compatibility.
@@ -557,6 +564,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 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.
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.
The fig
ref only exists if in Line 520 if figManager is None
.
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.
🐑
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.
😄
Current coverage is 62.07% (diff: 100%)@@ master #7023 diff @@
==========================================
Files 174 174
Lines 56021 56023 +2
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 34773 34776 +3
+ Misses 21248 21247 -1
Partials 0 0
|
Can you rebase this to and squash it to 1 or 2 commits? See http://matplotlib.org/devdocs/devel/gitwash/development_workflow.html#rebasing-a-pull-request-pr This needs a minimal test, using |
|
||
import matplotlib.pyplot as plt | ||
|
||
fg0 = plt.figure(num=1); |
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.
We use fig
, generally.
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.
Of course - thanks for the comments.
|
||
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 number exists. A new bool parameter `clear` was |
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.
number -> value
because it can be an arbitrary string.
print("fg0 is fg1: ", fg0 is fg1) | ||
print("fg1.texts: ", [t.get_text() for t in fg1.texts]) | ||
|
||
fg2, axx = plt.subplots(2, 1, num=1, clear=True) # clear contents |
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.
ax
import matplotlib.pyplot as plt | ||
|
||
fg0 = plt.figure(num=1); | ||
fg0.suptitle("A fancy plot"); |
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.
You can drop the semicolons as they do nothing.
# fg0 is fg2: True | ||
# fg2.texts: [] | ||
|
||
|
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.
Trim empty lines.
If False, suppress drawing the figure frame | ||
|
||
FigureClass : class derived from matplotlib.figure.Figure | ||
This parameter allows to optionally use a custom Figure instance. |
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.
Everything up to (and including) 'to' is kind of redundant.
@@ -462,6 +463,15 @@ def figure(num=None, # autoincrement if None, else integer from 1-N | |||
edgecolor : | |||
the border color. If not provided, defaults to rc figure.edgecolor | |||
|
|||
frameon : bool, optional, default: True | |||
If False, suppress drawing the figure frame |
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.
Missing trailing period.
You need to force push the rebased branch, not merge the remote branch into your local branch. |
I hope it's correct now - github and me are not best friends yet ... |
Looks good 😄 👍 |
@@ -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() |
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.
4-space indents, please.
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.
🐑
Thanks @vollbier for this patch! Sorry it took so long to be merged. Your PR suffered from the "it doesn't appear of the first page of reviews" effect :( |
@NelleV I guess there's still a bit of time until the 2.1 release left - so I don't see a problem. |
Adds the keyword
clf=False
toplt.figure()
. It clears the content of an already existing figure (being referenced by thenum
kwarg).The original motivation is stated in #6285. Another view would be: Instead of writing
fg = plt.figure(num=10); fg.clf()
, one can writefg = plt.figure(num=10, clf=True)
. Theclf()
is needed when the script is called repeatedly (with different parameters) in an interactive interpreter.The discussion if this is api bloat or not can be continued here. From a user perspective, it changes
to
which is imho much more readable.
Closes #6285.