Skip to content

[Bug]: Blitting + draw_event behaves inconsistently between QtAgg and TkAgg backends #29928 #30898

@mitra-gayathri

Description

@mitra-gayathri

Bug summary

When using blitting together with a draw_event callback, backend behavior differs between TkAgg and QtAgg.

Specifically:
fig.canvas.draw_artist(...) is backend-dependent and unreliable.
ax.draw_artist(...) works consistently across backends but still
Under TkAgg, using blitting with a draw_event callback causes visible flickering and repeated redraws.
The same code behaves smoothly under QtAgg, without flickering.

This indicates a backend-specific issue in TkAgg, where draw_event interactions interfere with blitting and trigger unnecessary redraw cycles, rather than a documentation problem.

Code for reproduction

import matplotlib
matplotlib.use("TkAgg") 

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 200)

fig, ax = plt.subplots()
(line,) = ax.plot(x, np.sin(x), animated=True)

def on_draw(event):
    print("draw_event fired")
    ax.draw_artist(line)

fig.canvas.mpl_connect("draw_event", on_draw)

plt.show(block=False)
plt.pause(0.1)

bg = fig.canvas.copy_from_bbox(fig.bbox)

for i in range(50):
    fig.canvas.restore_region(bg)
    line.set_ydata(np.sin(x + i * 0.2))
    ax.draw_artist(line)
    fig.canvas.blit(fig.bbox)
    fig.canvas.flush_events()
    plt.pause(0.05)

Actual outcome

When running the above script with TkAgg, the figure visibly flickers and redraws repeatedly during the animation.
The draw_event callback appears to be triggered more aggressively, interfering with blitting and causing unnecessary redraw cycles.

Image

Expected outcome

Blitting combined with a draw_event callback should behave consistently across backends.
Specifically: Using ax.draw_artist(...) inside a draw_event callback should not trigger additional full redraws.
The animation should render smoothly without flickering or flashing.
The number of draw_event callbacks should be comparable across backends (TkAgg and QtAgg).
Image

Additional information

No response

Operating system

Windows 10 / Windows 11

Matplotlib Version

import matplotlib; print(matplotlib.version)

Matplotlib Backend

TkAgg, QtAgg

Python version

python --version

Jupyter version

jupyter notebook version

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions