DSA-Assignments

Log | Files | Refs | README

problem1_generatePlot3d.py (1564B)


      1 import os
      2 import matplotlib.pyplot as plt
      3 from mpl_toolkits.mplot3d import Axes3D
      4 import numpy as np
      5 
      6 # Function to read measurements from a file
      7 def read_measurements(filename):
      8     measurements = []
      9     with open(filename, 'r') as file:
     10         for line in file:
     11             parts = line.strip().split(',')
     12             if len(parts) == 3:
     13                 measurements.append((int(parts[0]), int(parts[1]), float(parts[2])))
     14     return measurements
     15 
     16 # Get the current directory and construct the file path
     17 current_dir = os.getcwd()
     18 file_path = os.path.join(current_dir, 'problem1_data_insertionSort.txt')
     19 
     20 # Read measurements from file
     21 measurements = read_measurements(file_path)
     22 
     23 # Unpacking the measurements
     24 max_values, array_sizes, times = zip(*measurements)
     25 
     26 # Convert to numpy arrays for easier handling
     27 max_values = np.array(max_values)
     28 array_sizes = np.array(array_sizes)
     29 times = np.array(times)
     30 
     31 # Creating the 3D plot
     32 fig = plt.figure(figsize=(16, 12))  # Increase the figure size (width, height) in inches
     33 ax = fig.add_subplot(111, projection='3d')
     34 
     35 # Plotting
     36 scatter = ax.scatter(max_values, array_sizes, times, c=times, cmap='viridis', marker='o')
     37 
     38 # Adding labels and title
     39 ax.set_xlabel('Max Integer Value')
     40 ax.set_ylabel('Array Length')
     41 ax.set_zlabel('Time (seconds)')
     42 
     43 # Adding a color bar
     44 color_bar = fig.colorbar(scatter, ax=ax, extend='both')
     45 color_bar.set_label('Sorting Time (seconds)')
     46 
     47 plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
     48 
     49 plt.savefig(os.path.join(current_dir, '3d_plot.png'), dpi=500)
     50 
     51 # Show the plot
     52 plt.show()