关于python利用动态变量实时绘图的问题

julian_hzl 2018-07-27 11:30:45
利用python读取到mysql数据库中的实时数据(每秒钟输出一个值),想把这个值作为半径绘制圆,展示园的实时变化。但是在调试中遇到点小问题,不能实时变化。
...全文
937 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
julian_hzl 2018-07-27
  • 打赏
  • 举报
回复
# -*- coding:utf-8 -*-
import pymysql
import threading
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.patches import Circle

fig, ax = plt.subplots()
r=0

#python连接数据库操作
def t1():
global r
conn = pymysql.connect(
user="root",
password="123",
port=3306,
host="127.0.0.1", #本地数据库 等同于localhost
db="1", #数据库名
charset="utf8"
)
cur = conn.cursor() # 获取对应的操作游标

#获得数据库中的第一条数据
query = "select `range` from 00f92acd_detail where mac='ec:3d:fd:f9:2a:cd' GROUP BY master_id ORDER BY master_id DESC LIMIT 0,1 "
cur.execute(query)
r = cur.fetchone()
r = r[0]
r = float(r)

def animat(i):
global r
plt.cla()
cir = Circle(xy=(0.5, 0.5), radius=r / 100., alpha=0.5, fill=0)
ax.add_patch(cir)

#对函数def_t1()每一秒执行一次
def t2():
ani = animation.FuncAnimation(fig=fig, func=animat, frames=10000, interval=1000, blit=False)
while 1:
t1()
time.sleep(1)

if __name__ == '__main__':
t = threading.Thread(target=t2)
t.start()
plt.show()
# 此处写你主线程要处理的事情.....
t.join()

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧