我用串口监听一些数据。我在tkinter输出中显示这些值,并在pygame中完成显示板。一切正常。
现在我尝试将绘图仪窗口与pyplot集成。用户可以选择6个值中的2个值。 1-2分钟后,绘图仪速度变慢,我认为绘图仪将图形的旧值保存在某处。我该如何更好地解决这个问题。
import matplotlib.pyplot as plt
初始化
def initPlotter():
global showPlotterFlag
global lastPlotterChoice
global fevent
global fig
global ax1, ax2
global start
start = time.time()
plt.style.use('bmh')
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('mobile instruments plot')
plottertext = ["aaaaaaaaa", "bbbbbbbbbbbbbb",
"cccccccccc", "ddddddddd", "eeeeeee", "ffffffffff"]
plotterAxis = [[0, 5], [50, 150], [0, 6000], [0.7, 1.3], [50, 250], [8, 16]]
maxVal = 0
for i in range(6):
if lastPlotterChoice[i] == 1:
maxVal += 1
if (maxVal == 1):
#ax1.set_title(plottertext [i])
ax1.set_ylabel(plottertext [i])
ax1.set_ylim([plotterAxis[i][0], plotterAxis[i][1]])
if (maxVal == 2):
#ax2.set_title(plottertext [i])
ax2.set_ylabel(plottertext [i])
ax2.set_ylim(plotterAxis[i][0], plotterAxis[i][1])
if maxVal == 1:
ax2.set_ylabel('nicht benutzt')
fevent = plt.connect('close_event', handle_close)
plt.show(block=False)
showPlotterFlag=True
在主循环上:
def showPlotterDisplay():
global yps
global valueInArray
global valuesCurr
global ax1, ax2
global start
yps += 1
maxVal = 0
now = time.time() - start
for i in range(6):
if lastPlotterChoice[i] == 1:
maxVal += 1
if (maxVal == 1):
ax1.set_xlim([now-12, now+3])
ax1.plot(now, valueInArray[i], 'g.') # valueInArray[1] r+
if (maxVal == 2):
ax2.set_xlim([now-12, now+3])
ax2.plot(now, valueInArray[i], 'r.') # valueInArray[1] r+
答案 0 :(得分:0)
在matplotlib
中,每次plot
(或以任何方式绘制在轴上,例如使用imshow
)都不会清除先前的状态,而只是添加在那上面。
在长循环中,这会导致轴上的actor过载,从而减慢了代码的速度。
假设不需要在一个轴上同时绘制所有线条,一个可能的解决方案是在下一次迭代绘制数据之前,先ax.clear()
绘制轴。
另一方面,如果在一个轴上需要大量数据,则最好的选择是在非交互模式下工作(plt.ioff()
),plot
所需的一切,最后{{1} } /保存您的图形(这样,您将跳过所有中间渲染和重绘)。最终情节仍然很慢,但是中间步骤不会那么糟糕。