有沒有辦法讓我們可以在fig.show()Python 處理之后的例程時查看和使用繪圖結果(圖形和軸)?
例如,運行下面的代碼,Python 在處理 for 回圈時顯示圖形視窗而不是繪圖(它只顯示白色背景,滯后)。只有在整個代碼完成后,我才能看到情節并以互動方式使用它。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3]); fig.show()
#I want the plot to be visible and explorable,
# while the for loop below is in process (or any other kind of routine)
for i in range(10000):
print(i)
結果截圖(你可以看到情節滯后,只有空白的白色):

uj5u.com熱心網友回復:
您可以結合使用單獨的 python 行程(通過multiprocessing)和plt.show()的阻塞行為來實作所需的結果:
import matplotlib.pyplot as plt
import time
from multiprocessing import Process
def show_plot(data):
fig, ax = plt.subplots()
ax.plot(data)
plt.show()
def do_calculation():
for i in range(100):
print(i)
time.sleep(0.1)
if __name__ == '__main__':
p = Process(target=show_plot, args=([1,2,3],))
p.start() # start parallel plotting process
do_calculation()
p.join() # the process will terminate once the plot has been closed
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447257.html
標籤:Python matplotlib
