Blitting helps redraw only parts of a plot that change. This makes animations or updates faster and smoother.
Blitting for performance in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Matplotlib
fig, ax = plt.subplots() background = fig.canvas.copy_from_bbox(ax.bbox) # Draw static elements here fig.canvas.blit(ax.bbox) # Update dynamic elements fig.canvas.restore_region(background) # Draw updated elements ax.draw_artist(dynamic_element) fig.canvas.blit(ax.bbox) fig.canvas.flush_events()
copy_from_bbox saves the background image of the plot area.
blit redraws only the changed parts for better speed.
Examples
Matplotlib
import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() line, = ax.plot([], [], 'ro') ax.set_xlim(0, 10) ax.set_ylim(0, 10) background = fig.canvas.copy_from_bbox(ax.bbox) for x in range(10): fig.canvas.restore_region(background) line.set_data(x, x) ax.draw_artist(line) fig.canvas.blit(ax.bbox) fig.canvas.flush_events()
Matplotlib
fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) background = fig.canvas.copy_from_bbox(ax.bbox) # Static plot drawn once fig.canvas.blit(ax.bbox)
Sample Program
This program animates a sine wave moving horizontally. It uses blitting to update only the sine wave line, making the animation smooth and fast.
Matplotlib
import matplotlib.pyplot as plt import numpy as np import time plt.ion() # Turn on interactive mode fig, ax = plt.subplots() ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-1.5, 1.5) line, = ax.plot([], [], 'b-') x = np.linspace(0, 2 * np.pi, 100) # Draw static elements fig.canvas.draw() background = fig.canvas.copy_from_bbox(ax.bbox) for phase in np.linspace(0, 2 * np.pi, 60): fig.canvas.restore_region(background) y = np.sin(x + phase) line.set_data(x, y) ax.draw_artist(line) fig.canvas.blit(ax.bbox) fig.canvas.flush_events() time.sleep(0.05) plt.ioff() plt.show()
Important Notes
Blitting works best with simple animations where only small parts change.
Interactive mode (plt.ion()) helps to see updates live.
Not all backends support blitting; use one that does (like TkAgg).
Summary
Blitting redraws only changed parts of a plot for better speed.
It is useful for animations and interactive updates.
Use copy_from_bbox, restore_region, and blit methods together.
Practice
1. What is the main purpose of
blitting in matplotlib?easy
Solution
Step 1: Understand what blitting does
Blitting redraws only the parts of the plot that change, instead of the whole plot.Step 2: Compare options
Options B, C, and D describe unrelated tasks like 3D plotting, saving files, or color changes.Final Answer:
To redraw only the changed parts of a plot for faster updates -> Option AQuick Check:
Blitting = redraw changed parts only [OK]
Hint: Blitting means updating only what changes fast [OK]
Common Mistakes:
- Thinking blitting saves plots as files
- Confusing blitting with changing colors
- Assuming blitting creates 3D plots
2. Which of the following is the correct way to save the background region for blitting in matplotlib?
easy
Solution
Step 1: Identify correct method usage
Thecopy_from_bboxmethod is called on the figure canvas (fig.canvas) with the axes bounding box (ax.bbox).Step 2: Check options carefully
background = fig.canvas.copy_from_bbox(ax.bbox)is correct. Options B, C call it onax(which lacks the method), D calls it onfig(missing.canvas), B also uses wrong bbox.Final Answer:
background = fig.canvas.copy_from_bbox(ax.bbox) -> Option BQuick Check:
copy_from_bbox called on fig.canvas with ax.bbox [OK]
Hint: copy_from_bbox called on fig.canvas with ax.bbox [OK]
Common Mistakes:
- Calling copy_from_bbox on ax instead of fig.canvas
- Using fig.bbox instead of ax.bbox
- Mixing up ax and fig in method calls
3. What will the following code print?
import matplotlib.pyplot as plt fig, ax = plt.subplots() line, = ax.plot([0, 1], [0, 1]) background = fig.canvas.copy_from_bbox(ax.bbox) line.set_ydata([1, 0]) fig.canvas.restore_region(background) ax.draw_artist(line) print(line.get_ydata())
medium
Solution
Step 1: Trace the code changes
The line's y-data is changed to [1, 0] usingset_ydata.Step 2: Understand blitting steps
The background is restored, then the updated line is drawn. The line's data remains [1, 0].Final Answer:
[1 0] -> Option AQuick Check:
set_ydata changes line data to [1 0] [OK]
Hint: set_ydata changes data; restore_region redraws background [OK]
Common Mistakes:
- Assuming restore_region resets line data
- Confusing line data with original plot data
- Expecting an error from restore_region
4. You try to use blitting but your plot does not update visually after calling
draw_artist. What is the most likely mistake?medium
Solution
Step 1: Understand blitting update steps
After drawing the updated artist, you must callfig.canvas.blit(ax.bbox)to update the screen.Step 2: Analyze options
You forgot to callfig.canvas.blit(ax.bbox)afterdraw_artistcorrectly identifies the missing blit call. Options A and C describe incorrect method orders. You did not callplt.show()at the end is unrelated if running in interactive mode.Final Answer:
You forgot to call fig.canvas.blit(ax.bbox) after draw_artist -> Option DQuick Check:
Missing canvas.blit call stops visual update [OK]
Hint: Always call canvas.blit after draw_artist to update [OK]
Common Mistakes:
- Not calling canvas.blit after draw_artist
- Calling copy_from_bbox too late
- Confusing restore_region order
- Assuming plt.show fixes blitting updates
5. You want to animate a scatter plot with 1000 points updating their positions in real-time. Which approach using blitting will give the best performance?
hard
Solution
Step 1: Identify efficient blitting steps for animation
Best practice is to save the background once, then restore it each frame, update only the scatter points, draw them, and callcanvas.blit.Step 2: Compare other options
Redraw the entire scatter plot from scratch each frame without blitting redraws everything, which is slow. Only update the figure title text each frame using blitting updates only title text, not points. Useplt.pause()inside a loop without blitting uses pause without blitting, which is less efficient.Final Answer:
Save background with copy_from_bbox, update scatter offsets, restore background, draw scatter, then call canvas.blit -> Option CQuick Check:
Blitting updates only changed scatter points fast [OK]
Hint: Save background once, restore, update points, then blit [OK]
Common Mistakes:
- Redrawing entire plot each frame
- Updating only title text instead of points
- Using plt.pause without blitting for speed
