-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve performance of plt.clf() #26243
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
Where does the |
Thanks for the quick review, resetting the spine is indeed necessary. Not sure whether this is enough (I am still setting up tests locally), but the performance improvement seems worthwhile. |
I'd just like to point out #26164. |
@tacaswell Tests are passing now. The PR assumes the axis in the spines objects are the same as in
|
@@ -1275,7 +1275,11 @@ def __clear(self): | |||
for axis in self._axis_map.values(): | |||
axis.clear() # Also resets the scale to linear. | |||
for spine in self.spines.values(): | |||
# avoid expensive methods in the clearing of the spine, gh-26243 | |||
tmp_axis = spine.axis | |||
spine.axis = 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.
We have a context manager that will do this setting/resetting in matplotlib.cbook._setattr_cm
.
@eendebakpt I think that the way this is addressed in #26164 is a better way to address this as it avoids patching monkeying with spine state (at the cost of a new private method). |
I tested #26164 with the benchmark script from this PR and the performance gain is comparable. If that PR is merged, I will close this one. |
Closing in favor of #26164 |
PR summary
This PR improves performance of
plt.clf()
. By settingspines.axis
toNone
a double call toXAxis.clear()
andYAxis.clear()
is avoided.Benchmark script:
Performance on main:
Performance on PR
Also see #23771
PR checklist