0

I am trying to do a dynamically updating plot in Matplotlib Python 3.x using Tkinter. It starts with displaying a file dialog for the user to select the .csv file.

Here's the example of the .csv file: sample CSV File

I want to plot each row then the plot is updated & plotting the next row.

Here's what I currently have:

plt.style.use('fivethirtyeight')
xs =[]
ys = []
csvFile = filedialog.askopenfile(mode='r', filetypes=(("CSV file", "*.csv"), ("All files", "*.*")), title="Select a CSV file")
csvFile2 = pd.read_csv(csvFile, header=[2])
selectedColumn = csvFile2.iloc[0:, 3:]
selectedArray = selectedColumn.to_numpy() # arrays
def animate():
 for row in selectedArray:
 plt.clf() #clear current figure
 plt.plot(row)
 plt.title('Plot')
 plt.xlabel('Radar No')
 plt.ylabel('Value')
 plt.ylim(0.2, 0.9) # Keeping the y axis stays the same during the loop
 plt.draw()
 plt.pause(0.0078) #0.0078 if the frequency is 128Hz -- Idk about this one
 plt.show()
animate()

It does plot the numbers dynamically, but the fps is so slow, about 5 fps.

Therefore, I am looking for another method, Funcanimation, but I am not sure how to use it. Inside the variable Selectedarray is something like this:

[0.489377 0.481563 0.477656 ... 0.300366 0.294261 0.288156] [0.489866 0.48254 0.478633 ... 0.300855 0.294994 0.288645] [0.489377 0.481319 0.478144 ... 0.300122 0.293773 0.288156]

....

I believe using Funcanimation is faster and I could control the speed(?) Could anyone pls help.

Thank you.

asked Jul 17, 2020 at 6:35
3
  • Have you tried using fig.canvas.flush_events()? Something like this: line.set_ydata(row) fig.canvas.draw() fig.canvas.flush_events(next row) plt.show() ... Commented Jul 17, 2020 at 6:42
  • No, I haven't, but I will try it then. Thanks heaps :) Commented Jul 17, 2020 at 6:45
  • Oh I think you don't have to input your next row in flush_events. I'll try to write a short general answer below, it's hard without concrete data. Commented Jul 17, 2020 at 6:56

1 Answer 1

1

There's a way doing these things with the flush_events() method. I'll try to give a general answer here, may need to adapt a little bit for the specific data and needs. Also, consider using time.sleep() to control for the speed of figure update.

import pandas as pd
import matplotlib.pyplot as plt
import time
myarray = pd.read_excel("yourdata.xlsx", index_col=None)
fig, ax = plt.subplots(1)
i=0
for row in myarray.head().itertuples():
 print(row)
 if i == 0:
 line, = ax.plot(row)
 else:
 line.set_ydata(row)
 fig.canvas.draw()
 fig.canvas.flush_events()
 plt.show()
 i += 1
 time.sleep(0.5)
answered Jul 17, 2020 at 7:00
2
  • Thanks for your response. I tried to use this on the code, the graph is plotted, but it isn't dynamically changing, that means I still have to modify the code a bit, right? Commented Jul 17, 2020 at 7:27
  • I edited the answer based on a simple excel file with numeric data. It works for that sort of data structure. You may need to remove your column names or replace them by integers going from 0 to N columns (maybe, please check). Commented Jul 17, 2020 at 7:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.