Skip to content

Conversation

@scottshambaugh
Copy link
Contributor

@scottshambaugh scottshambaugh commented Jan 22, 2026

PR summary

Towards #5665

Currently we are calling _update_ticks() 3 times per axis every draw call, and _get_ticklabel_bboxes 2 times per axis. These calls take up about 35% of total draw time for an empty plot.

We can eliminate 25% of total draw time by caching the results of these calculations every draw cycle. This introduces some state, but I think it's decently well guarded and the performance boost is definitely worth it.

Before & After
The circled red areas show the before & after runtime of axis._update_label_position within Axis.draw(). It runs after the cache values have been set by other functions, so we nearly completely eliminate its runtime.

Before:
image

After:
image

Profiling script:

import time
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

print("Timing...")
start_time = time.perf_counter()
for i in range(100):
    fig.canvas.draw()
end_time = time.perf_counter()

plt.close()
print(f"Time taken: {end_time - start_time:.4f} seconds")

PR checklist

@scottshambaugh scottshambaugh marked this pull request as ready for review January 22, 2026 02:32
@scottshambaugh scottshambaugh mentioned this pull request Jan 22, 2026
1 task
@scottshambaugh scottshambaugh changed the title PERF: Cache ticks and tick label bboxes each draw cycle PERF: Cache ticks and ticklabel bboxes within each draw cycle Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant