我一直在使用Python的绘图库,最近发现了一个经过实践证明的成熟库matplotlib。然而,在创建一个简单的图表时,我遇到了一个问题。
在下面的例子中,Dummy 类的 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)
ax.plot(...)
。 - Thorsten Kranz