Here's the updated code that plots boxes as lines with delta values on the x-axis and averages on the y-axis:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the TSV file
df = pd.read_csv('average_deltas_by_box.tsv', sep='\t')
# Get the delta columns
delta_cols = [col for col in df.columns if col.startswith('delta')]
# Extract delta values from column names (e.g., 'delta10' -> 10)
delta_values = []
for col in delta_cols:
delta_val = int(col.replace('delta', ''))
delta_values.append(delta_val)
# Prepare for plotting
plt.figure(figsize=(12, 8))
# Generate colors for each box
num_boxes = len(df)
colors = plt.cm.hsv(np.linspace(0, 1, num_boxes))
# Plot each box as a line
for i, row in df.iterrows():
box_label = f"Box [{row['box_start']}, {row['box_end']})"
y_values = [row[col] for col in delta_cols]
plt.plot(delta_values, y_values, label=box_label, color=colors[i], marker='o', linestyle='-')
# Add labels, title, and grid
plt.xlabel('Delta')
plt.ylabel('Average Value')
plt.title('Average Values by Delta for Each Box')
plt.grid(True)
# Position the legend outside the plot on the right side with 2 columns
plt.legend(bbox_to_anchor=(1.02, 0.5), loc='center left', ncol=2, fontsize='small')
# Adjust layout to make room for the legend
plt.tight_layout()
plt.subplots_adjust(right=0.75)
# Save the plot
plt.savefig('boxes_as_lines_plot.png', bbox_inches='tight')
plt.show()
print(f"Plot saved as 'boxes_as_lines_plot.png' with {len(df)} box lines.")
This code:
- Extracts the numeric delta values from column names (e.g., 'delta10' becomes 10)
- For each box (row in the dataframe), plots a line where:
- X-axis: delta values (0, 10, 20, ..., 500)
- Y-axis: the average values for that box across all deltas
- Each line represents a different box with its range shown in the legend
- Uses different colors for each box line to distinguish them
- Places the legend on the right side in two columns
The resulting plot shows how the average values change as delta increases, with each line representing a different sliding_scores box range.