0

I have a file "00.csv", which can be updated each new cycle (the file 00.csv: is the result of a reading of another file, which extracts to the latter two values each time (each cycle).

here is the 00.csv file:

0, -1184.645675411964
1, -1184.653778859924 # first cycle
2, -1184.657325034754 # second cycle
3 -1184.657972735058
4, -1184.658582481392
5, -1184.658800896487
6, -1184.658844384850
7, -1184.658846846248
8, -1184.658825508352 # eighth cycle

I created a simple script, in order to draw a graph animate.

import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib.animation as animation

fig = plt.figure()
ax1 = fig.add_subplot(111)


def animate(i):
  graph_data = open('00.csv', 'r') .read()
  lines = graph_data.split('\n')
  xs = []
  ys = []
  for line in lines:
    if len(line) > 1:
      x, y = line.split(',')
      xs.append(x)
      ys.append(y)
  ax1.clear()
  ax1.plot(xs, ys , color='blue', linestyle='dashdot', linewidth=4, marker='o', markerfacecolor='red', markeredgecolor='black',markeredgewidth=3, markersize=12)

ani = animation.FuncAnimation (fig , animate, interval=1000 )


#plt.legend(loc='upper right', fancybox=True)
plt.savefig("Plot_SCF-Energy_.pdf", dpi=150)
plt.savefig("Plot_SCF-Energy_.png", dpi=150)
plt.show()

the result (at a certain moment: eighth cycle) is as follows: enter image description here

my problem is: after the eighth cycle, other cycles are added (i mean: both columns x, y) to 00.csv file like this:

9, -1184.658861339248   # ninth cycle
10, -1184.658863735214  # tenth cycle
11, -1184.658862250518  #  eleventh cycle

but the graph remains frozen in eighth cycle, it does not update !! ???

in addition,I followed Matplotlib create real time animated graph, but I did not solve my problem.

Is there a way to automatically read the file 00.csv after new cycle, in order to refresh my graph ??

Hamza allal
  • 101
  • 2
  • 3
  • 11

1 Answers1

0

The values you plot here seem to be strings instead of numbers. That does not make any sense.

Convert them to numbers first before trying to plot them.

xs.append(float(x))
ys.append(float(y))

You may also consider using some more efficient method to read the data.

import numpy as np

def animate(i):
    x,y = np.genfromtxt('00.csv', delimiter=",", unpack=True, invalid_raise=False)
    ax1.clear()
    ax1.plot(x, y)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • thank you, I tried with your script, but I achieved the same result. after this attempt I realized that the problem is not focusing on the ploting graph, but it's about my file 00.csv. because I realized that "00.csv" does not update automatically. – Hamza allal Dec 23 '17 at 10:23