一直在玩Python的繪圖庫,並遇到了matplotlib,似乎已經進行了戰鬥測試和驗證。然而,我在線程中創建一個簡單的情節時遇到了一個問題。matplotlib繪製在簡單的線程中凍結
在本例中波紋管,虛擬類plotme方法是在一個線程連續運行兩次,但它可以在第二次迭代卡/凍結。 最有可能是明顯的,並與線程本身有關,但我沒有發現它迄今。
import matplotlib.pyplot as plt
from numpy import arange, sin, pi
import threading
class Dummy():
def plotme(self, iteration = 1):
print "%ix plotting... " % iteration,
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
#savefig("test.png") # irrelevant here
plt.close()
def threadme(self, iteration = 1):
thread_plot = threading.Thread(target=self.plotme,
args=(iteration,))
thread_plot.start()
thread_plot.join()
dummy = Dummy()
dummy.threadme(1)
dummy.threadme(2)
您是否知道在matplotlib.pyplot的命令不是安全的?您應該使用OOÜP方法,例如創建一個圖形,一個軸對象,然後調用該軸對象上的方法,例如, 'ax.plot(...)' –