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

2024-09-30 12:15:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设置一个网关,它每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')

这是去2号站的

#!/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()

提前谢谢你的帮助

抱歉,如果这太乱了


Tags: toimporthostdataserverdefasciifig

热门问题