简体   繁体   中英

Python using Matplotlib for real-time plotting

在此处输入图片说明 I'm going to preface this by saying that I am still learning Python so please be kind and patient. My code goes as follows:

  • Client on the network sends a text file (stats.txt) every ~5 seconds to an SCP server. Python code sits on the server.

Code below begins:

import matplotlib.pyplot as plt
import csv
import datetime


x = []
y = []
rssi_val = []



def animate(i):
    with open('stats.txt', 'r') as searchfile:
        time = (searchfile.read(5))
        for line in searchfile:
            if 'agrCtlRSSI:' in line:
                rssi_val = line[16:20]

    y = [rssi_val]

    x = [time for i in range(len(y))]

    plt.xlabel('Time')
    plt.ylabel('RSSI')
    plt.title('Real time signal strength seen by client X')
    #plt.legend()


    plt.plot(x,y)

    ani = FuncAnimation(plt.gcf(), animate, interval=5000)

    plt.tight_layout()

    #plt.gcf().autofmt_xdate()

    plt.show()
  • SCP server opens the file every 5 seconds and plots the values that are parsed from the file. Time gets plotted on the X axis and the RSSI value gets plotted on the Y axis.

I understand that the code and methods used are not efficient at this point and will be modified in the future. For now I simply want the the plot values to show up and the chart to be animated with the plot (line) every 5 or so seconds.

Running it produces nothing.

You need to have the line

ani = FuncAnimation(plt.gcf(), animate, interval=5000)

Outside of the function animate , then assuming the data are received and read in properly you should see the plot updating. You may also need to put plt.show() after the FuncAnimation() line depending on how you are executing the script.


Edit

You may want to try something like this instead

import matplotlib.pyplot as plt
import csv
import datetime

x = []
y = []
rssi_val = []

def animate(i):
    with open('stats.txt', 'r') as searchfile:
        time = (searchfile.read(5))
        for line in searchfile:
            if 'agrCtlRSSI:' in line:
                rssi_val = line[16:20]

    y.append(rssi_val)
    x.append(time)

    plt.cla()
    plt.plot(x,y)
    plt.xlabel('Time')
    plt.ylabel('RSSI')
    plt.title('Real time signal strength seen by client X')
    plt.tight_layout()

ani = FuncAnimation(plt.gcf(), animate, interval=5000)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM