[問題] matplotlib.pyplot關不起來

看板Python作者 (Dana)時間6年前 (2019/03/21 13:22), 6年前編輯推噓2(208)
留言10則, 5人參與, 6年前最新討論串1/1
from PIL import Image import matplotlib.pyplot as plt im = Image.open("./1.jpg") plt.figure("1") plt.imshow(im) #繪製圖片 plt.show() #顯示圖片 plt.close() #關閉圖片 程式碼如上 路徑沒有問題 圖片可以正常顯示 但沒辦法關閉圖片 只能自己手動關閉 才會繼續跑下一行 環境是用anaconda安裝的 使用vscode執行 有看過plt.close可以執行的參數 都試過了 想請教問題出在哪裡 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.56.155 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1553145749.A.CA9.html plt.show() is a blocking function, so in the example code you used above, plt.close() isn't being executed until the window is closed, which makes it redundant. You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications. 有查到預設是blocking function 所以並不會繼續執行直到被手動關閉 若是加入指令plt.ion變成交互模式 反而不會顯示圖片 所以目前就會呈現一個狀態 可以自動關閉圖片 但不會顯示圖片 或是得手動關閉圖片 自動顯示下一張 但是圖片出的來 ※ 編輯: alen84204 (114.32.56.155), 03/21/2019 13:38:22

03/21 15:43, 6年前 , 1F
plt.show(block=False)
03/21 15:43, 1F

03/21 15:43, 6年前 , 2F
time.sleep(5)
03/21 15:43, 2F

03/21 15:43, 6年前 , 3F
plt.close('all')
03/21 15:43, 3F

03/21 15:44, 6年前 , 4F
03/21 15:44, 4F
https://imgur.com/SP1zlpr.jpg
有試過關閉非交互模式 問題是只要在交互模式下 plt.show(block=False) 就會呈現沒有回應的狀態 問題依舊無法解決 感謝您的回覆 ※ 編輯: alen84204 (114.32.56.155), 03/22/2019 10:10:55

03/23 08:42, 6年前 , 5F
交互模式是指 python console嗎?
03/23 08:42, 5F

03/23 11:06, 6年前 , 6F
你的問題是完全沒寫過程式 這跟數學習題不一樣
03/23 11:06, 6F

03/23 11:07, 6年前 , 7F
沒有所謂的標準答題方式 你這樣是學不會的
03/23 11:07, 7F

03/23 11:07, 6年前 , 8F
推錯篇 抱歉
03/23 11:07, 8F

03/23 14:33, 6年前 , 9F
QwQ 可惡 還以為被說中沒寫過程式 我只有寫過fortran
03/23 14:33, 9F

04/04 13:18, 6年前 , 10F
用plt.pause(1) +plt.close('all') 然後不要plt.show
04/04 13:18, 10F
文章代碼(AID): #1San-Lof (Python)
文章代碼(AID): #1San-Lof (Python)