大多數關于matplotlib專案影片的問題都詳細說明了以線格式繪制資料,這似乎不適用于影像。
我想在子圖中顯示三個系列的影像,并為圖形設定影片以顯示系列中的每個專案。目前,我使用子圖創建圖形,并在for回圈中生成要在影片中顯示的圖形串列。我正在使用 jupyter 筆記本,因此我將其匯出到 jshtml 框架。我的代碼:
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3,
figsize=(12, 6))
fis = []
for i in range(0,len(filename_list),4): #show every 4th image in series
ax0.set_title('Raw image')
ax0.imshow(plt.imread(filename_list[i]),animated=True)
ax1.set_title('Manually cropped image')
ax1.imshow(plt.imread( (filename_list[i].parent / 'cropped' /filename_list[i].name) ) ,animated=True)
ax2.set_title('Cropped image after re-centering')
ax2.imshow(plt.imread( (filename_list[i].parent / 'cropped_corr' /filename_list[i].name) ) ,animated=True)
fis.append([fig])
ani = animation.ArtistAnimation(fig, fis, interval=500, blit=False, repeat_delay=1000)
plt.close()
HTML(ani.to_jshtml())
這會產生一個有效的 jshtml 影片,但子圖不會改變。如何設定影片以便子圖更新?我不知道如何制作更新每一幀的影片功能。
謝謝!
uj5u.com熱心網友回復:
發布后不久,我重新檢查了在影片中僅創建一張影像的代碼。我突然想到我需要將每個imshow命令設定為添加到數字串列中的變數。代碼變成:
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3,
figsize=(12, 6))
fis = []
for i in range(0,len(filename_list),4):
ax0.set_title('Raw image')
im0=ax0.imshow(plt.imread(filename_list[i]),animated=True)
ax1.set_title('Manually cropped image')
im1=ax1.imshow(plt.imread( (filename_list[i].parent / 'cropped' /filename_list[i].name) ) ,animated=True)
ax2.set_title('Cropped image after re-centering')
im2=ax2.imshow(plt.imread( (filename_list[i].parent / 'cropped_corr' /filename_list[i].name) ) ,animated=True)
fis.append([im0,im1,im2])
ani = animation.ArtistAnimation(fig, fis, interval=500, blit=False, repeat_delay=1000)
plt.close()
HTML(ani.to_jshtml())
注意添加im0,im1,im2給每個imshow呼叫,在fis.append命令來創建一個串列的串列。
uj5u.com熱心網友回復:
from IPython.display import clear_output
fis = []
for i in range(0,len(filename_list),4): #show every 4th image in series
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(12, 6), num = 1, clear = True)
ax0.set_title('Raw image')
ax0.imshow(plt.imread(filename_list[i]),animated=True)
ax1.set_title('Manually cropped image')
ax1.imshow(plt.imread( (filename_list[i].parent / 'cropped' /filename_list[i].name) ) ,animated=True)
ax2.set_title('Cropped image after re-centering')
ax2.imshow(plt.imread( (filename_list[i].parent / 'cropped_corr' /filename_list[i].name) ) ,animated=True)
plt.show()
fis.append([fig])
clear_output()
ani = animation.ArtistAnimation(fig, fis, interval=500, blit=False, repeat_delay=1000)
HTML(ani.to_jshtml())
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317800.html
標籤:Python matplotlib
上一篇:Matplotlib:使用Python3.9的云函式中的自定義字體
下一篇:按色調孵化seaborn計數圖
