Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
eendebakpt wants to merge3 commits intomatplotlib:mainfromeendebakpt:clf

Conversation

eendebakpt
Copy link
Contributor

@eendebakpteendebakpt commentedJul 3, 2023
edited
Loading

PR summary

This PR improves performance ofplt.clf(). By settingspines.axis toNone a double call toXAxis.clear() andYAxis.clear() is avoided.

Benchmark script:

import timeimport matplotlibfrom matplotlib import axisimport matplotlib.pyplot as pltprint(matplotlib.__version__)def go():t0=time.perf_counter()for ii in range(5):plt.figure(100)plt.clf()plt.plot(range(20), range(20,40), label='hi')plt.legend()dt=time.perf_counter()-t0return dtniter=10x=[]for ii in range(niter):    dt=go()    print(f'round {ii}: {dt:2f} [s]')    x.append(dt)    print(f'mean: {sum(x)/niter:.2f} [s]')

Performance on main:

3.8.0.dev1461+gf017315dd5round 0: 0.329322 [s]round 1: 0.173158 [s]round 2: 0.196067 [s]round 3: 0.167451 [s]round 4: 0.170085 [s]round 5: 0.169859 [s]round 6: 0.203656 [s]round 7: 0.167369 [s]round 8: 0.170247 [s]round 9: 0.166873 [s]mean: 0.19 [s]

Performance on PR

3.8.0.dev1463+gbff26576f7round 0: 0.276565 [s]round 1: 0.088187 [s]round 2: 0.089832 [s]round 3: 0.089987 [s]round 4: 0.111984 [s]round 5: 0.089566 [s]round 6: 0.086931 [s]round 7: 0.087903 [s]round 8: 0.090093 [s]round 9: 0.085896 [s]mean: 0.11 [s]

Also see#23771

PR checklist

@tacaswell
Copy link
Member

Where does theaxis on the spine get re-set to notNone?

@eendebakpt
Copy link
ContributorAuthor

Where does theaxis on the spine get re-set to notNone?

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.

@oscargus
Copy link
Member

I'd just like to point out#26164.

@eendebakpteendebakpt changed the titleDraft: improve performance of plt.clf()Improve performance of plt.clf()Jul 4, 2023
@eendebakpt
Copy link
ContributorAuthor

@tacaswell Tests are passing now. The PR assumes the axis in the spines objects are the same as inself._axis_map. If this is not always the case, we could also do something like:

        cleared_axis = list( self._axis_map.values() )        for spine in self.spines.values():            # avoid expensive methods in the clearing of the spine, gh-26243                        tmp_axis = spine.axis            if tmp_axis in cleared_axis:                spine.axis = None            else:                 cleared_axis.append(tmp_axis)            spine.clear()            spine.axis = tmp_axis

@@ -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
Copy link
Member

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 inmatplotlib.cbook._setattr_cm.

@tacaswell
Copy link
Member

@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).

@eendebakpt
Copy link
ContributorAuthor

@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.

@eendebakpt
Copy link
ContributorAuthor

Closing in favor of#26164

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tacaswelltacaswelltacaswell left review comments

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@eendebakpt@tacaswell@oscargus@melissawm

[8]ページ先頭

©2009-2025 Movatter.jp