Yes, it's possible to add vertical lines from the x-axis ticks to make the plot more readable. We can use ax.axvline() to add these lines. Here's the modified version of the code with vertical lines at the tick marks:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Assuming prefiller_sent is your DataFrame
prefilled_sends = prefiller_sent[prefiller_sent['prefill_size'] > 0]
# Create figure with 3 subplots vertically
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 15))
# Function to create ECDF plot
def create_ecdf_plot(ax, data, unit, divisor, title):
sns.ecdfplot(data=data, x="prefill_size", log_scale=True, stat='percent', ax=ax)
ax.set_title(title)
ax.set_xlabel(f'Prefill Size ({unit})')
# Get current x-axis limits in bytes
xlim = ax.get_xlim()
# Create new tick positions and labels based on the unit
if unit == 'Bytes':
ticks = [1024, 1024*1024, 1024*1024*1024] # 1KB, 1MB, 1GB
tick_labels = ['1KB', '1MB', '1GB']
elif unit == 'KB':
ticks = [1, 1024, 1024*1024] # 1KB, 1MB, 1GB in KB
tick_labels = ['1KB', '1MB', '1GB']
elif unit == 'MB':
ticks = [1/1024, 1, 1024] # 1KB, 1MB, 1GB in MB
tick_labels = ['1KB', '1MB', '1GB']
# Convert ticks to the appropriate unit
ticks_in_unit = [t/divisor for t in ticks]
ax.set_xticks(ticks_in_unit)
ax.set_xticklabels(tick_labels)
# Add vertical lines at tick positions
for tick in ticks_in_unit:
ax.axvline(x=tick, color='gray', linestyle='--', alpha=0.5, ymin=0, ymax=1)
# Add grid for better readability
ax.grid(True, which="both", ls="-", alpha=0.2)
# Plot 1: Bytes
create_ecdf_plot(ax1, prefilled_sends, 'Bytes', 1,
'Cumulative Distribution of Prefill Sizes (Bytes)')
# Plot 2: Kilobytes
create_ecdf_plot(ax2, prefilled_sends, 'KB', 1024,
'Cumulative Distribution of Prefill Sizes (Kilobytes)')
# Plot 3: Megabytes
create_ecdf_plot(ax3, prefilled_sends, 'MB', 1024*1024,
'Cumulative Distribution of Prefill Sizes (Megabytes)')
# Adjust layout to prevent overlap
plt.tight_layout()
# Show the plot
plt.show()
The key changes made to add vertical lines are:
- Added this code block within the
create_ecdf_plot function, just before the grid:
# Add vertical lines at tick positions
for tick in ticks_in_unit:
ax.axvline(x=tick, color='gray', linestyle='--', alpha=0.5, ymin=0, ymax=1)
The parameters for axvline() control the appearance of the lines:
x=tick: Places the line at the tick position
color='gray': Sets the line color
linestyle='--': Makes the line dashed
alpha=0.5: Makes the line semi-transparent