New to python so any guidance would be appreciated!
I have a CSV that has names. I'm trying to count everybody with the name "Adam" and plot it in a bar chart. When the CSV gets updated I want the bar chart to update as well. Below is my code.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import csv
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
myCount = 0
otherCount = 0
def animate(i):
with open('test2.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['fName'] == 'Adam':
myCount +=1
else:
otherCount = +=1
x = ["Adam","Not Adam"]
y = [myCount, otherCount]
ax1.clear()
ax1.bar(x,y)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Right now it shows me the outline of the graph but doesn't display anything.
Also, I got the animate function from another source. Is there a more efficient way to do this?
Ok So I've updated the code:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import csv
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
myCount = 0
otherCount = 0
with open('testcsv.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['fName'] == 'Adam':
myCount +=1
else:
otherCount +=1
x = ["Adam","Not Adam"]
y = [myCount, otherCount]
ax1.clear()
ax1.bar(x,y)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
I know through testing that it's correctly grabbing the CSV and reading the data, but it's still not plotting it. The data looks like this:
fname,lname
Adam,Smith
Adam,Sandler
Adam,Smith
Adam,Sandler
Bruce,Willis
Adam,Smith
James,Harden
Bruce,Wayne
There seem to be three main problems with the code. The first two have to do with the counters in use. The last with the naming of the data.
otherCount = +=1 is not valid python.
Incrementing a variable locally, does not change it globally. It would hence make sense to define myCount = 0; otherCount = 0 inside the animating function, in case you do not want to show cumulative counts.
The data column seems to be named fname, but the indexing uses row['fName'].
Related
I've got this script running to update every time a new value is added to my csv file (it's a manual log taking in values from a machine):
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
counter=0
index = count()
def animate(i):
data = pd.read_csv('C:/Users/Owner/Downloads/Manual-Log-27Nov2020-150846.csv')
data=data.dropna()
data['Date And Time']=data['Date']+' '+data['Time']
Date = data['Date And Time']
Temp = data['Temperature']
Pressure = data['Pressure']
pH = data['pH']
plt.cla()
plt.plot(Date, Temp, label='Temperature')
plt.plot(Date, Pressure, label='Pressure')
plt.plot(Date, pH, label='pH')
plt.legend(loc='upper left')
plt.xlabel('Date and Time')
plt.ylabel('Y Values')
plt.xticks(rotation=90)
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=30000)
plt.tight_layout()
plt.show()
Since it's such a large file (20k lines or so) the resulting graph is huge and you can't really read the data properly. Is there a way I could only get it to show the 20 most recent readings?
you can slice it or get first n records. you can apply the following after your code line : data=data.dropna()
df=pd.DataFrame({'Count':[2,33,4,6,8,9],'apha':['A','B','C','D','E','F']})
df_sorted=sorted_df = df.sort_values(by=['Count'], ascending=True) # in case needed
df_limited=df_sorted.head(3) # this is one you are looking for
This Python program plots lines dynamically from data in a csv file. When the program first starts it dynamically draws points that already exist in the file. This part works as expected. I'd like for any new points added to the file to be subsequently drawn. The problem is that i continues to increment so by the time a new item is added to my csv file the value of i is usually much higher than the index from the csv so it never gets plotted. How can I prevent the count of i continuing on until there is an applicable value in the csv file?
import numpy as np
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
data = pd.read_csv('csv_data.csv')
x_vals = []
y_vals1 = []
y_vals2 = []
index = count()
def animate(i):
x = data['x_value']
y1 = data['total_1']
y2 = data['total_2']
x_vals.append(x[i])
y_vals1.append(y1[i])
y_vals2.append(y2[i])
plt.cla()
plt.plot(x_vals, y_vals1, label='Channel 1')
plt.plot(x_vals, y_vals2, label='Channel 2')
plt.legend(loc='upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=100)
plt.show()
I recently wrote this to scrape a log and show a matplotlib.pyplot.bar plot of the most used words in it
import re
from datetime import datetime
from collections import Counter
import matplotlib.pyplot as plt
from matplotlib import animation
def read_log(path, index, separator=chr(9)):
data = []
my_file = open(path,"r+")
rows = my_file.readlines()
for row in rows:
line = re.sub(r'\r\n|\r|\n','',row, flags=re.M)
if line != '':
data.append(line.split(separator)[index])
my_file.close()
return Counter(data)
def set_plot(counter_data):
plt.title('This is a title')
plt.bar(range(len(counter_data)), list(counter_data.values()), align='center')
plt.xticks(range(len(counter_data)), list(counter_data.keys()))
plt.tight_layout()
plt.show()
counter_data = read_log(r'logfile.txt',2)
print(counter_data)
set_plot(counter_data)
I would love to animate said plot, however, I can't grasp animation.FuncAnimation()
Can you help me out?
I added these lines:
fig = plt.Figure()
animation.FuncAnimation(fig, set_plot(counter_data), frames=20)
and deleted plt.show()
So I could give FuncAnimation an empty figure (fig) and the function. But it doesn't work. EDIT: And it doesn't print an error either.
It seems your data is static (you get it from file once and it doesn't change), so I don't really understand what you are trying to animate. But, your code contains errors that need to be fixed, so for demonstration purposes I will add increment each of the heights in each step of animation.
The first mistake is in the way you pass arguments to your function. For arguments you have to use fargs parameter, otherwise in your version you are passing the result of function not the function itself.
You must have a function (animate in my version, set_plot in yours) that updates the plot for each step of your animation. (in your case you just put the same data every time)
That function needs to accept at least one parameter (val) which is used my FuncAnimation which passes values got from iterator passed to its frames parameter.
The final code looks like this
import re
from datetime import datetime
from collections import Counter
import matplotlib.pyplot as plt
from matplotlib import animation
# uncomment if using in jupyter notebook
# %matplotlib nbagg
def read_log(path, index, separator=chr(9)):
data = []
my_file = open(path,"r+")
rows = my_file.readlines()
for row in rows:
line = re.sub(r'\r\n|\r|\n','',row, flags=re.M)
if line != '':
data.append(line.split(separator)[index])
my_file.close()
return Counter(data)
fig = plt.figure()
ax = fig.add_subplot()
counter_data = read_log(r'tmp.csv',2)
plt.title('This is a title')
bar = ax.bar(range(len(counter_data)), list(counter_data.values()), align='center')
plt.xticks(range(len(counter_data)), list(counter_data.keys()))
plt.tight_layout()
plt.ylim((0, 30))
def animate(val, counter_data):
data = list(counter_data.values())
for i in range(len(data)):
bar[i].set_height(data[i]+val)
animation.FuncAnimation(fig, func=animate, frames=20, fargs=[counter_data], save_count=10)
and we get the following animation:
Edit:
For errors you can try to save your animation to gif, and the errors will show up
anim = animation.FuncAnimation(fig, func=animate, frames=20, fargs=[counter_data], save_count=10)
anim.save('anim.gif', 'imagemagick')
The main problem is that FuncAnimation expects a callable which returns artist objects. The callable will be called repeatedly with a frame argument.
In your example, set_plot() is called once. It's return value (None) is passed to FuncAnimation. Instead you should have a method, e.g. update_plot(), which loads the data from the file, updates the bar plot and returns the bar plot. This function (the function itself) should be passed to FuncAnimation
animation.FuncAnimation(fig, update_plot, frames=20)
without calling it! Note the missing parenthesis after update_plot. The animitation documentation shows examples how this can be done.
I'm plotting real-time data using parsed data from a file that is being repeatedly opened. I'm deriving and plotting two different values on the same chart. The Y axis scales up and down according the values of each. The "snr" (green) value is plotting fine but the "data_rate"(red) value seems to be static. See screenshot.
import matplotlib.pyplot as plt
import csv
import datetime
from matplotlib.animation import FuncAnimation
import numpy as np
x = []
y = []
z = []
rssi_val = []
def animate(i):
with open('stats.txt', 'r') as searchfile:
time = (searchfile.read(8))
for line in searchfile:
if 'agrCtlRSSI:' in line:
rssi_val = line[16:20]
rssi_val=int(rssi_val)
if 'agrCtlNoise:' in line:
noise_val = (line[16:20])
noise_val=int(noise_val)
if 'maxRate:' in line:
data_rate = (line[16:20])
data_rate=int(data_rate)
snr = ((noise_val - rssi_val) * -1)
#begin test
y.append(snr)
x.append(time)
z.append(data_rate)
#end test
#begin test
plt.cla()
#Verify values are not empty
print("SNR = ", snr)
print("DR = ", data_rate)
plt.plot(snr,label='Signal')
plt.plot(data_rate,label='Data Rate')
plt.legend(loc='upper right')
plt.plot(x,y,z)
ax=plt.gca()
ax.tick_params('x',labelrotation=60)
#end test
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.show()
The simplest way is to call plot twice.
plt.plot(x,y)
plt.plot(x,z)
See "Plotting multiple sets of data" here for more options.
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()