python的plot如何实时更新_Python GUI中的实时绘图

我正在尝试编写一个Python GUI,我需要做一个实时情节.我目前有一个程序从我正在使用的机器接收数据,我希望能够在收到机器时输出机器输出的值.我一直在研究,到目前为止,我发现它似乎不像tkinter或任何库可以在GUI中执行此操作.有没有人知道tkinter是否以及如何做到这一点,或者是否有另一个能够进行这种实时情节的图书馆?

另外,在收到数据时,如何将收集的数据写入文件?

在此先感谢您的帮助.

解决方法:

看起来您通过轮询获取数据,这意味着您不需要线程或多个进程.只需在首选界面上轮询设备并绘制单点.

这是一个示例,其中包含一些模拟数据来说明一般概念.它每100ms更新一次屏幕.

import Tkinter as tk

import random

class ServoDrive(object):

# simulate values

def getVelocity(self): return random.randint(0,50)

def getTorque(self): return random.randint(50,100)

class Example(tk.Frame):

def __init__(self, *args, **kwargs):

tk.Frame.__init__(self, *args, **kwargs)

self.servo = ServoDrive()

self.canvas = tk.Canvas(self, background="black")

self.canvas.pack(side="top", fill="both", expand=True)

# create lines for velocity and torque

self.velocity_line = self.canvas.create_line(0,0,0,0, fill="red")

self.torque_line = self.canvas.create_line(0,0,0,0, fill="blue")

# start the update process

self.update_plot()

def update_plot(self):

v = self.servo.getVelocity()

t = self.servo.getTorque()

self.add_point(self.velocity_line, v)

self.add_point(self.torque_line, t)

self.canvas.xview_moveto(1.0)

self.after(100, self.update_plot)

def add_point(self, line, y):

coords = self.canvas.coords(line)

x = coords[-2] + 1

coords.append(x)

coords.append(y)

coords = coords[-200:] # keep # of points to a manageable size

self.canvas.coords(line, *coords)

self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":

root = tk.Tk()

Example(root).pack(side="top", fill="both", expand=True)

root.mainloop()

标签:export-to-csv,python,user-interface,matplotlib,tkinter

来源: https://codeday.me/bug/20190825/1722825.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值