I was trying to use matplotlib to replot an image in a faster way, so instead of replotting everything, I use set_data method of the AxesImage class as below:
import numpy as np
import time
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
img = plt.imshow(np.random.rand(100, 100))
img.set_data(np.random.rand(100, 100))
ax.draw_artist(ax.patch)
ax.draw_artist(img)
fig.canvas.update()
fig.canvas.flush_events()
I encountered this error:
Traceback (most recent call last): ... ax.draw_artist(ax.patch) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_base.py", line 2319, in draw_artist raise AttributeError(msg) AttributeError: draw_artist can only be used after an initial draw which caches the render
However, if I run the script line by line in python shell (IPython), it works and nothing is wrong. So what is the mystery behind this cached renderer?
EDIT: add a line fig.canvas.draw() solves the problem, now the remaining question is why running it line by line in IPython shell does not cause the same error?