用matplotlib绘制动态折线图

里面是一些链接好的文章

  1. Python–matplotlib绘图可视化知识点整理
  2. matplotlib事件处理基础(事件绑定、事件属性
  3. Python借助keyboard监听键盘事件
import random
import keyboard
from matplotlib import pyplot as plt

class rand():
    # 随机数类
    def __init__(self,num):
        self.index = 1
        self.num =num
        self.x =[]
        self.list_y =[]
        [self.list_y.append([]) for i in range(self.num)]

    def flag(self,event):
        a = keyboard.KeyboardEvent('down', 28, 'A')
        if event.event_type == 'down' and event.name == a.name:
            self.index = -self.index
            print(self.index)


    def rand_num(self):
        #生成数据 一秒更新一次
        [self.list_y[i].append(random.randrange(0,10))  for i in range(self.num)]
        self.x.append(len(self.list_y[0]))

    def show_plt(self):
        color = ['green','red','blue','skyblue']
        keyboard.hook(self.flag)   #本质是多线程
        while True:
            if self.index == 1:
                self.rand_num()
                [plt.plot(self.x,self.list_y[i], color=color[i%4]) for i in range(self.num)]
                plt.xlabel('site')
                plt.ylabel('length')
                plt.pause(1.5)
if __name__ == '__main__':
    plt_show = rand(5)    # 在此更改数据组数
    plt_show.show_plt()

你可能感兴趣的:(python,python)