使用Matplotlib 以非阻塞方式繪圖:解決執行問題
當嘗試使用Matplotlib 繪製函數而不停止執行時,您可能會遇到與GUI 和後端互動相關的困難。一個常見的誤解是 show(block=False) 的用法,它可能並不總是有效,這取決於後端。
您提供的程式碼示範了 show(block=False) 的用法,但看起來您是在呼叫 show() 之前不使用 plt.ion()。 plt.ion() 啟動互動模式,允許非阻塞繪圖,您可以在其中繪製和暫停更新。
啟用 plt.ion() 後,您可以呼叫 plt.show() 顯示繪圖視窗。該視窗將保持開啟且非阻塞狀態,讓您繼續執行。但是,要查看更新後的繪圖,您需要使用 plt.draw() 重新繪製畫布,並使用 plt.pause(timeout) 暫停執行一小段時間。 plt.pause() 中的逾時參數決定了暫停的長度(以秒為單位)。
在您的特定情況下,還需要注意的是,每次調用 plt.plot() 時,都會產生一個新繪圖在同一視窗中建立。如果您想要更新現有繪圖,則應使用 plt.plot(x, y, color='r')。這將以紅色繪製新資料點,同時保留現有繪圖。
以下是程式碼的修訂版本,應解決非阻塞問題並更新現有繪圖:
<code class="python">import matplotlib.pyplot as plt import numpy as np def main(): plt.ion() # Activate interactive mode plt.show() # Display the plot window plt.axis([-50,50,0,10000]) x = np.arange(-50, 51) for pow in range(1,5): y = [Xi**pow for Xi in x] plt.plot(x, y, color='r') # Update the plot in red color plt.draw() plt.pause(0.001) # Pause for a brief period input("Press [enter] to continue.") if __name__ == '__main__': main()</code>
以上是如何使用 Matplotlib 以非阻塞方式繪圖:為什麼 `show(block=False)` 並不總是有效?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3漢化版
中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。