Got it — you're saying:
- The raw chunk lengths are too noisy (too many unique lengths).
- You want to group (bin) the chunk lengths into two broad categories:
- 1–100 characters
- 100+ characters
- Then plot a clean bar chart comparing tokenizers across these two bins.
✅ That makes a lot of sense — it will massively simplify the visualization.
Here’s how you can do it:
Updated Code: Binned Bar Plot
import matplotlib.pyplot as plt
import pandas as pd
# Step 1: Prepare the data
data = []
for name, lengths in chunk_lens.items():
for l in lengths:
# Bin the chunk lengths
if l <= 100:
bin_label = "1-100"
else:
bin_label = "100+"
data.append({"Tokenizer": name, "Bin": bin_label})
df = pd.DataFrame(data)
# Step 2: Group by Tokenizer and Bin
grouped = df.groupby(["Tokenizer", "Bin"]).size().reset_index(name="Count")
# Step 3: Pivot for plotting
pivoted = grouped.pivot(index="Tokenizer", columns="Bin", values="Count").fillna(0)
# Step 4: Plot
pivoted.plot(kind="bar", stacked=True, figsize=(10, 6), color=["skyblue", "salmon"])
plt.title("Chunk Lengths Binned (1-100 vs 100+)")
plt.xlabel("Tokenizer")
plt.ylabel("Number of Chunks")
plt.xticks(rotation=45)
plt.legend(title="Chunk Length Bin")
plt.tight_layout()
plt.show()
What this does:
- Bins each chunk into either "1-100" or "100+".
- Counts how many chunks fall into each bin for each tokenizer.
- Plots a stacked bar chart:
- Lower part = number of short chunks (1–100 chars)
- Upper part = number of long chunks (100+ chars)
Example of what the plot will show:
| Tokenizer | 1-100 | 100+ |
|---|
| tok1 | 500 | 200 |
| tok2 | 300 | 400 |
| tok3 | 600 | 100 |
- If a tokenizer has mostly short chunks, the blue part will dominate.
- If a tokenizer has lots of long chunks, the red part will dominate.
Bonus: If you want side-by-side bars (not stacked)
Just change:
pivoted.plot(kind="bar", stacked=True, figsize=(10, 6), color=["skyblue", "salmon"])
to
pivoted.plot(kind="bar", stacked=False, figsize=(10, 6), color=["skyblue", "salmon"])
✅ This will make the "1-100" and "100+" bars appear next to each other for each tokenizer.
Summary
This approach:
- Bins the data into two simple categories.
- Avoids messy overlapping.
- Makes it super easy to compare tokenizers.
Would you also want a version that shows percentages (normalized counts) instead of raw counts?
(e.g., "80% of chunks are 1-100" vs "20% are 100+")
→ I can show you that too if you want! 🚀