minimum code to reproduce
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots(figsize = (5, 5))
fig.canvas.draw()
len1 = len(np.frombuffer(fig.canvas.buffer_rgba(), dtype='uint8'))
plt.pause(0.01)
len2 = len(np.frombuffer(fig.canvas.buffer_rgba(), dtype='uint8'))
fig.canvas.draw()
len3 = len(np.frombuffer(fig.canvas.buffer_rgba(), dtype='uint8'))
plt.pause(0.01)
len4 = len(np.frombuffer(fig.canvas.buffer_rgba(), dtype='uint8'))
print(len1, len2, len3, len4)
output: 1000000 2250000 2250000 2250000
expected: 1000000 1000000 1000000 1000000
Question
length of canvas buffer changed after I call plt.pause, and do not change even if I call another draw method. Is this a bug or is it designed this way?
Background (my purpose)
I am trying to make simple animation using draw()
method. Instead of FuncAnimation
API, this solution has better control when you want to stop the animation (plt.ion()
enabled, so some computations can run in background). So, with plt.ion()
, I need to call plt.pause
so that it does not block the entire thread.
Now when I want to turn the animation to video, I try to acquire the buffer of the canvas, and face this “buffer size question”
Environment
tested in two case:
- Python 3.10.14 + matplotlib 3.9.0
- Python 3.11.5 + matplotlib 3.7.2
SYLG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.