Python matplotlib动画和Tkinter中的性能缓慢
我在Tkinter应用程序中使用Matplotlib动画(来自Arduino的串行端口数据)时遇到了一些问题,除了高性能桌面之外,我觉得我在做一些效率低下的事情 这个应用程序按照我的要求运行,所有的东西都显示正确,只是有点滞后。根据测试,这似乎是matplotlib实现的一个问题,当动画运行时,一切都会变慢。我曾尝试在动画中打开blitting,但这似乎没有什么不同。当它只是一个没有图形的Tkinter时,它就很快了 另一方面,当我只运行图形(即,没有包装在gui中)时,一切似乎都比较顺利。这可能是TkAgg后端吗?或者是Tkinter在与资源竞争,而图形正在吞噬这条线Python matplotlib动画和Tkinter中的性能缓慢,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我在Tkinter应用程序中使用Matplotlib动画(来自Arduino的串行端口数据)时遇到了一些问题,除了高性能桌面之外,我觉得我在做一些效率低下的事情 这个应用程序按照我的要求运行,所有的东西都显示正确,只是有点滞后。根据测试,这似乎是matplotlib实现的一个问题,当动画运行时,一切都会变慢。我曾尝试在动画中打开blitting,但这似乎没有什么不同。当它只是一个没有图形的Tkinter时,它就很快了 另一方面,当我只运行图形(即,没有包装在gui中)时,一切似乎都比较顺利。这可
import matplotlib
import Tkinter as tk
import ttk
import tkFileDialog
import serial
import io
import os
import matplotlib.animation as animation
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import style
from serialFunctions import findArduino #custom function to identify arduino from com port address
funcanimation的matplotlib代码:
fig = Figure()
ax = fig.add_subplot(111)
line, = ax.plot(np.random.rand(10), color = 'red', label = 'Velocity (cm/s)')
sio = io.TextIOWrapper(io.BufferedRWPair(arduino, arduino, 1))
#Data Update
tmin = 0.0
tmax = 10.0
def init():
line.set_data([], [])
return line,
def run(data):
t, y = data
#print t , y
xdata.append(t)
ydata.append(y)
line.set_data(xdata, ydata)
if t >= tmax- 1.00:
line.axes.set_xlim(t - tmax + 1.0, t + 1.0)
return line,
def data_gen():
'''Generator to pass data to the run function via funcanimation'''
t = 0
while True:
t+=0.1
try:
dat = float(sio.readline())
# print dat
except:
dat = 0
yield t, dat
ani = animation.FuncAnimation(fig, run, data_gen, init_func = init, interval=20, blit=False)
Tkinter相关代码:
matplotlib.use('TkAgg')
class StreamPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, {'bg': '#020b19'})
label = tk.Label(self, text = 'Begin Streaming', font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
backButton = ttk.Button(self, text = 'Back',
command = lambda:
controller.show_frame(StartPage))
backButton.pack()
canvas = FigureCanvasTkAgg(fig, self)
canvas.show()
canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = True)
#toolbar = NavigationToolbar2TkAgg(canvas, self)
#toolbar.update()
canvas._tkcanvas.pack(side = tk.TOP, fill = tk.BOTH, expand = True)
阿杜伊诺密码
int const potPin = A0;
int potVal;
void setup() {
// put your setup code here, to run once:
analogReference(DEFAULT); //EXTERNAL NO MORE THAN 5V!
Serial.begin(1200);
}
void loop() {
// put your main code here, to run repeatedly:
potVal = analogRead(potPin);
float voltage = potVal * (5.0/1024.0);
Serial.print(voltage);
Serial.print('\n');
}
我也遇到了同样的性能问题,并在这里找到了很好的建议:
在run函数中调用update_idletasks显著提高了我的性能。我也遇到了同样的性能问题,并在这里找到了很好的建议:
在run函数中调用update_idletasks可以显著提高我的性能。“性能缓慢”不是一个好的问题描述。试着找出代码的哪一部分花费的时间最多,然后把你的问题集中在那一部分。很抱歉,我对这一部分做了一些改进,它似乎是动画的实现。当此选项处于活动状态时,动画本身和Tkinter都会立即减速,并变得非常无响应。分离后,每个组件都工作良好。好像每个组件都在争夺处理时间。请添加运行代码所需的所有“导入”。。。另外,我不久前使用matplotlib绘制了一个大3D绘图。当我切换到时,性能有了很大的提高;如果你换了,也许你的表现会更好?(需要python 2.7。)我已经为此添加了所有导入。我没听说过myavi,但我会查一查。您是否将此与Tkinter集成?“性能迟缓”不是一个好的问题描述。试着找出代码的哪一部分花费的时间最多,然后把你的问题集中在那一部分。很抱歉,我对这一部分做了一些改进,它似乎是动画的实现。当此选项处于活动状态时,动画本身和Tkinter都会立即减速,并变得非常无响应。分离后,每个组件都工作良好。好像每个组件都在争夺处理时间。请添加运行代码所需的所有“导入”。。。另外,我不久前使用matplotlib绘制了一个大3D绘图。当我切换到时,性能有了很大的提高;如果你换了,也许你的表现会更好?(需要python 2.7。)我已经为此添加了所有导入。我没听说过myavi,但我会查一查。你把它和Tkinter结合起来了吗?