problem
I have a problem with matplotlib. I looked at a lot of things online (including an extensive amount of stack overflow questions) and did not find an answer to my problem. To be clear: I know how to dynamically update a plot in matplotlib. Everything I find online deals with showing the plot AFTER having set the series. here the problem is different. Please see the code below.
I have simulation results being written on the fly in a file (let's say file.log
). I have a current code that prints the results on the fly as well using matplotlib. the problem is that, for various reasons, sometimes a new variable is written in file.log
. But the monitoring is already displayed. How can I add a lineplot to the existing plot and keep updating ?
Here I give the general structure of the code I have.
The code structure I have
shell command: I tail the log file one line by one line and pipe the output to a python script which handles the line in a for loop (this is working fine, no problem with that).
tail -f -n 1 file.log | monitoring.py
The python. It is commented line by line. Sorry for the long code sequence but everything with matplotlib is a bit long and needs context
#here I load the previous data (before starting monitoring)
historic_data = function_to_load_history('path/to/file.log')
#then I create a plot
figure, ax = plt.subplots()
#I keep the lines in a list
lineList = []
for serie in historic_data:
line, = ax.plot(serie)
lineList.append(line)
#then I display the plot
plt.ion()
plt.show()
#and I start 'listening' for tail input in the for loop
for line in sys.stdin:
# I add the new data to the historic data
historic_data = function_to_append(line, historic_data)
#I update the plot and redraw, using the already created lines
for i,serie in enumerate(historic_data):
# OK here is the problem : here, sometimes, a new serie is discovered in the log file
# so the last series have no line associated in lineList. I test this :
# if the line exist in the line list, then no problem I update an existing line
if i < len(lineList):
lineList[i].set_data(serie)
#HERE IS THE PROBLEM : if i enconter a new serie, I add it to the plot but it is not dislayed.
else:
line, = ax.plot(serie)
lineList.append(line)
figure.gca().autoscale_view()
figure.gca().relim()
plt.draw()
I have no idea how to ask matplotlib to draw the new serie(s). Any idea ? thank you =)