problem1_generatePlot2d.py (1338B)
1 import os 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 # Function to read measurements from a file 6 def read_measurements(filename): 7 measurements = [] 8 with open(filename, 'r') as file: 9 for line in file: 10 parts = line.strip().split(',') 11 if len(parts) == 3 and int(parts[0]) == 9000: # Check if the first value is 1000 12 measurements.append((int(parts[1]), float(parts[2]))) # Ignore the first value 13 return measurements 14 15 # Get the current directory and construct the file path 16 current_dir = os.getcwd() 17 file_path = os.path.join(current_dir, 'problem1_data_insertionSort.txt') 18 19 # Read measurements from file 20 measurements = read_measurements(file_path) 21 22 # Unpacking the measurements 23 array_sizes, times = zip(*measurements) # Only two values now 24 25 # Convert to numpy arrays for easier handling 26 array_sizes = np.array(array_sizes) 27 times = np.array(times) 28 29 # Creating the 2D plot 30 plt.figure(figsize=(16, 12)) # Adjust figure size 31 32 # Plotting 33 plt.scatter(array_sizes, times, c='blue', marker='o') # Use a single color for simplicity 34 35 # Adding labels and title 36 plt.xlabel('Array Length') 37 plt.ylabel('Time (seconds)') 38 39 plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) 40 41 plt.savefig(os.path.join(current_dir, '2d_plot_filtered.png'), dpi=500) 42 43 # Show the plot 44 plt.show()