Reputation: 75
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:
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()
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.
Upvotes: 2
Views: 3589
Reputation: 10330
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.
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()
Upvotes: 1