我一直试图使用pyplot / matplotlib来显示图像,因为它们在循环中发生了变化,但是我一直无法使任何东西正常工作。我基本上无法更新显示的图像。这是复制问题的代码:
f = plt.figure(1)
ax = plt.gca()
show_obj= ax.imshow(np.random.randn(28,28))
for i in range(10):
print(i)
# None of these 3 options work
if True:
# the image does not update
show_obj.set_data(np.random.randn(28,28))
f.canvas.draw()
if False:
# image does not update
ax.clear()
ax.imshow(np.random.rand(28,28))
pass
if False:
# starts drawing new axes
f = plt.figure(1)
ax = plt.gca()
ax.imshow(np.random.rand(28,28))
plt.show()
答案 0 :(得分:3)
我测试了这段代码,它可以工作。
from IPython.display import clear_output
import matplotlib.pyplot as plt
from numpy.random import randn
from time import sleep
for i in range(5):
clear_output()
plt.imshow(randn(28, 28))
plt.show()
sleep(1)
答案 1 :(得分:1)
灵感来自 korsakov 的帖子;这种修改使它更“流畅”,也更合乎逻辑:-)
from IPython.display import clear_output
import matplotlib.pyplot as plt
from numpy.random import randn
from time import sleep
for i in range(5):
plt.imshow(randn(28, 28))
plt.show()
sleep(1)
clear_output(wait=True)
答案 2 :(得分:0)
如果要显示输出中的所有图像,可以运行:
import matplotlib.pyplot as plt
for filename in filenames:
img = plt.imread(filename)
plt.imshow(img)
plt.show()