0

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 =)

tomJ
  • 1
  • 1
  • Dose this https://stackoverflow.com/a/16446688/9475509 meet you need? – dudung Jan 20 '23 at 09:28
  • @dudung Thanks, but, as explained, my problem is different. In your link,, all the plot series are created BEFORE plotting. this woul not be a problem for me. – tomJ Jan 20 '23 at 09:34
  • Could you just create the other series but do not show them, so it just a matter to hide it or display it with different or the same color with background. That's only what I can think right now. Sorry. – dudung Jan 20 '23 at 10:37
  • @dudung As I understand it your idea is to create empty series and, if needed, set data to it. It is a good idea but it is a workaround and I do not know the number of series before trying to plot... thank you anyway =) – tomJ Jan 20 '23 at 11:00

0 Answers0