plot with seaborn

This commit is contained in:
2025-02-28 20:34:32 +01:00
parent 6998598f29
commit f4e29c18e5

View File

@@ -1,5 +1,7 @@
import seaborn as sns
import matplotlib.pyplot as plt
import yaml
import pandas as pd
# Function to read and parse the file
def read_metrics(file_path):
@@ -8,47 +10,47 @@ def read_metrics(file_path):
return data['runs']
# Read metrics from the file
file_path = 'project/metrics.yaml' # Replace with the correct file path
file_path = 'project/metrics.yaml' # Replace with your file path
runs = read_metrics(file_path)
# Extracting values
n_tasks = [run['n_task'] for run in runs]
build_times = [float(run['build']['components_build_time']) for run in runs]
gen_times = [float(run['code_gen']['gen_time']) for run in runs]
deploy_times = [float(run['deploy']['components_deploy_time']) for run in runs]
#Flatten data into a list of dictionaries
data = []
for run in runs:
if 'build' in run:
data.append({'Task': run['n_task'], 'Type': 'Build Time', 'Time': float(run['build']['components_build_time'])})
# Plotting Build Time
plt.figure(figsize=(8, 6))
ax = plt.gca() # Get current axis
p = ax.bar(n_tasks, build_times, color='b')
ax.bar_label(p, label_type='edge')
ax.set_title('Build Time')
ax.set_xlabel('Number of Tasks')
ax.set_ylabel('Build Time (seconds)')
plt.tight_layout()
plt.savefig('build_time_plot.png') # Save the plot as an image
plt.close()
if 'code_gen' in run:
data.append({'Task': run['n_task'], 'Type': 'Generation Time', 'Time': float(run['code_gen']['gen_time'])})
# Plotting Code Generation Time
plt.figure(figsize=(8, 6))
ax = plt.gca() # Get current axis
p = ax.bar(n_tasks, gen_times, color='g')
ax.bar_label(p, label_type='edge')
ax.set_title('Code Generation Time')
ax.set_xlabel('Number of Tasks')
ax.set_ylabel('Generation Time (seconds)')
plt.tight_layout()
plt.savefig('gen_time_plot.png') # Save the plot as an image
plt.close()
if 'deploy' in run:
data.append({'Task': run['n_task'], 'Type': 'Deployment Time', 'Time': float(run['deploy']['components_deploy_time'])})
# Plotting Deployment Time
plt.figure(figsize=(8, 6))
ax = plt.gca() # Get current axis
p = ax.bar(n_tasks, deploy_times, color='r')
ax.bar_label(p, label_type='edge')
ax.set_title('Deployment Time')
ax.set_xlabel('Number of Tasks')
ax.set_ylabel('Deployment Time (seconds)')
plt.tight_layout()
plt.savefig('deploy_time_plot.png') # Save the plot as an image
plt.close()
# Convert to DataFrame
df = pd.DataFrame(data)
# Function to plot and add median labels
def plot_metric(metric, filename, color):
subset = df[df['Type'] == metric]
plt.figure(figsize=(8, 6))
ax = sns.boxplot(x='Task', y='Time', data=subset, color=color)
# Add median labels
medians = subset.groupby('Task')['Time'].median()
for i, task in enumerate(medians.index):
median_value = medians[task]
ax.text(i, median_value, f'{median_value:.3f}', ha='center', va='center',
fontsize=10, color='white', bbox=dict(facecolor='black', alpha=0.6, boxstyle='round,pad=0.3'))
plt.title(f'{metric} by Number of Tasks')
plt.xlabel('Number of Tasks')
plt.ylabel('Time (seconds)')
plt.tight_layout()
plt.savefig(filename)
plt.close()
# Plot each metric separately
plot_metric('Build Time', 'res/build_time_boxplot.png', 'skyblue')
plot_metric('Generation Time', 'res/gen_time_boxplot.png', 'lightgreen')
plot_metric('Deployment Time', 'res/deploy_time_boxplot.png', 'salmon')
print('Plots saved successfully!')