如何将时间间隔设置为10毫秒,并且matplotlib仍然可以设置动画?

时间:2019-02-07 19:43:13

标签: python matplotlib

我正在建立一个网关,该网关每10毫秒通过TCP连接从两个站点读取一次读数。网关应使用matplotlib显示每个站点的读数,并计算这些读数的平均值并显示出来。

当我在设置时间间隔= 1000的三个图上对其进行测试时,动画图工作正常。但是,当我将时间间隔设置为10时,未显示任何子图。当我将其设置为500时,一开始它可以工作,但是当工作站连接到网关后,动画会冻结

由于需要,我需要将间隔设为10毫秒

这是第1站的

#!/usr/bin/env python3
import socket
from random import randint
#import time

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 4444        # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))  # connecting to local server
    while True:
        data = randint(1, 10)
        data = str(data)  # user input
        s.send(data.encode('ascii'))  # encoding the user input then sending to server
        data = s.recv(1024)
        data = data.decode('ascii')

这是第二站的

#!/usr/bin/env python3

import socket
from random import randint
#import time

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 5555        # The port used by the server
#time.sleep(.00000001)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))  # connecting to local server
    while True:
        data = randint(1, 10)
        data = str(data)  # user input
        s.send(data.encode('ascii'))  # encoding the user input then sending to server
        data = s.recv(1024)
        data = data.decode('ascii')
        #print (data)

这是网关

#!/usr/bin/env python3
#
#
import _thread
import socket
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation


HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT1 = 4444  # Port to listen on (non-privileged ports are > 1023)
PORT2 = 5555
Station1Data = 0
Station2Data = 0
average = 0


def server1():
    s =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT1))
    s.listen()
    conn, addr = s.accept()
    with conn:
        while True:
            data = conn.recv(1024)
            data = data.decode('ascii')
            conn.send(data.encode('ascii'))
            #print(data)
            global Station1Data
            Station1Data = int(data)


def server2():
    s =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT2))
    s.listen()
    conn, addr = s.accept()
    with conn:
        while True:
            data = conn.recv(1024)
            data = data.decode('ascii')
            conn.send(data.encode('ascii'))
            global Station2Data
            Station2Data = int(data)


def average():
    global Station1Data, Station2Data, average
    while True:
        average = (Station1Data+Station2Data)/2







try:
    _thread.start_new_thread(server1, ())
    _thread.start_new_thread(server2, ())
    _thread.start_new_thread(average,())

except:
    print("Error: unable to start thread")

fig = plt.figure()
#plt.xlabel('time')
#plt.ylabel('Mw                Mw               Mw')
ax1 = fig.add_subplot(3,1,1)
x = 19
xs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
ys = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
ax2 = fig.add_subplot(3,1,2)
x2 = 19
xs2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
ys2 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
ax3 = fig.add_subplot(3,1,3)
x3=19
xs3 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
ys3 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]


def animate(i):
    global x,xs,ys,Station1Data
    x = x+1
    xs.append(x)
    ys.append(Station1Data)
    xs = xs[-20:]
    ys = ys[-20:]
    ax1.clear()
    ax1.set_xticklabels([])
    ax1.plot(xs, ys,'b')
    ax1.set_title('Station1')


def animate2(i):
    global x2,xs2,ys2,Station2Data
    x2 = x2+1
    xs2.append(x2)
    ys2.append(Station2Data)
    xs2 = xs2[-20:]
    ys2 = ys2[-20:]
    ax2.clear()
    ax2.set_xticklabels([])
    ax2.plot(xs2, ys2,'r')
    ax2.set_title('Station2')


def animate3(i):
    global x3,xs3,ys3,average
    x3 = x3+1
    xs3.append(x3)
    ys3.append(average)
    xs3 = xs3[-20:]
    ys3 = ys3[-20:]
    ax3.clear()
    ax3.set_xticklabels([])
    ax3.plot(xs3, ys3,'g')
    ax3.set_title('average')


animation1 = animation.FuncAnimation(fig, animate, interval=10)
animation12 = animation.FuncAnimation(fig, animate2, interval=10)
animation13 = animation.FuncAnimation(fig, animate3, interval=10)

plt.show()

预先感谢您的帮助

对不起,如果这很麻烦

0 个答案:

没有答案