import sys import re import os import pandas as pd import matplotlib.pyplot as plt def generate_grouped_bar_graph(file_path1, file_path2, kind, output_directory): # Extracting information from file names pattern = r'(\d+_\d+_\d+_\d+_\d+_\d+)_(PCP|PPCP)_(\d+)\.xmlv3\.csv' match1 = re.match(pattern, os.path.basename(file_path1)) match2 = re.match(pattern, os.path.basename(file_path2)) if not match1 or not match2: print("Error: Invalid file names.") sys.exit(1) common_info = match1.group(1) task_num = match1.group(3) # Read CSV files into DataFrames df1 = pd.read_csv(file_path1) df2 = pd.read_csv(file_path2) # Check if both DataFrames have the same number of rows if len(df1) != len(df2): print("Error: Both CSV files must have the same number of rows.") sys.exit(1) # Combine the data for grouped bar graph df_combined = pd.concat([df1['task'], df1[kind], df2[kind]], axis=1, keys=['Task', 'PCP', 'PPCP']) # Plot grouped bar graph plt.figure(figsize=(10, 6)) df_combined.plot(x='Task', kind='bar', ax=plt.gca()) # Increase font size for title and axes labels title_fontsize = 16 # Adjust this size as needed axes_label_fontsize = 14 # Adjust this size as needed plt.title(f'Grouped {kind.capitalize()} Values', fontsize=title_fontsize) plt.xlabel('Task', fontsize=axes_label_fontsize) plt.ylabel(kind.capitalize(), fontsize=axes_label_fontsize) plt.legend(title='Protocol') # Adjust the tick parameters plt.xticks(rotation=0, fontsize=12) # Adjust fontsize as needed plt.yticks(fontsize=12) # Adjust fontsize as needed # Adjusting grid and removing margins plt.grid(axis='y', linestyle='--', alpha=0.7) plt.margins(x=0, y=0) # This reduces the margins around the plot # We want the plot to be tightly fitted plt.tight_layout() # Save the plot as a PNG figure output_file_path = os.path.join(output_directory, f'{common_info}_{kind}_{task_num}.png') plt.savefig(output_file_path) #plt.show() if __name__ == "__main__": if len(sys.argv) < 4 or sys.argv[3] not in ['best', 'worst', 'average', 'missed']: print("Usage: python script.py best|worst|average|missed [output directory]") sys.exit(1) file_path1 = sys.argv[1] file_path2 = sys.argv[2] kind = sys.argv[3] output_directory = sys.argv[4] if len(sys.argv) > 4 else '.' generate_grouped_bar_graph(file_path1, file_path2, kind, output_directory)