Skip to content

Commit 62133ea

Browse files
committed
Continuously plot and save predator-prey population chart
1 parent a306f6f commit 62133ea

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Stats/plot_predator_prey.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import matplotlib.pyplot as plt
33
import argparse
4+
import time
45

56
def 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

3132
def 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

4352
if __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

Comments
 (0)