11import re
22import matplotlib .pyplot as plt
33import argparse
4+ import time
45
56def parse_simulation_output (file_path ):
67 cow_counts = []
@@ -29,7 +30,7 @@ def parse_simulation_output(file_path):
2930 return cow_counts , tiger_counts , tree_counts , grass_counts
3031
3132def plot_predator_prey (cows , tigers , grass ):
32- plt .figure ( figsize = ( 10 , 5 ))
33+ plt .clf () # Clear the current figure
3334 plt .plot (range (len (cows )), cows , label = 'Cows (Prey)' , color = 'blue' )
3435 plt .plot (range (len (tigers )), tigers , label = 'Tigers (Predator)' , color = 'red' )
3536 plt .plot (range (len (grass )), grass , label = 'Grass (Food Source)' , color = 'green' )
@@ -38,12 +39,19 @@ def plot_predator_prey(cows, tigers, grass):
3839 plt .title ('Predator-Prey-Resource Population Dynamics' )
3940 plt .legend ()
4041 plt .grid ()
41- plt .show ()
42+ plt .draw ()
43+ plt .savefig ('./plot.png' , format = 'png' )
44+ plt .pause (3 ) # Pause to update the plot every 3 seconds
45+
46+ def continuously_plot (filepath ):
47+ plt .ion () # Enable interactive mode
48+ while True :
49+ cow_population , tiger_population , _ , grass_population = parse_simulation_output (filepath )
50+ plot_predator_prey (cow_population , tiger_population , grass_population )
4251
4352if __name__ == "__main__" :
4453 parser = argparse .ArgumentParser (description = "Parse simulation output and plot predator-prey-resource dynamics." )
4554 parser .add_argument ("filepath" , type = str , help = "Path to the simulation output file" )
4655 args = parser .parse_args ()
4756
48- cow_population , tiger_population , _ , grass_population = parse_simulation_output (args .filepath )
49- plot_predator_prey (cow_population , tiger_population , grass_population )
57+ continuously_plot (args .filepath )
0 commit comments