Spaces:
Sleeping
Sleeping
Update tab/tab5_randomness_visualizer.py
Browse files
tab/tab5_randomness_visualizer.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# ---------- Function to plot bit distribution ----------
|
| 7 |
def plot_key_bits(key_str):
|
|
@@ -14,21 +15,32 @@ def plot_key_bits(key_str):
|
|
| 14 |
diff = abs(zero_count - one_count)
|
| 15 |
total_bits = len(bits)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
# Color code bits: Blue for 0, Orange for 1
|
| 21 |
colors = ['#1f77b4' if b == 0 else '#ff7f0e' for b in bits]
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Add user-friendly titles
|
| 25 |
ax.set_title("🔍 Visual Check: 0s vs 1s in Your QKD Key", fontsize=12)
|
| 26 |
ax.set_xlabel("Bit Position in Key", fontsize=10)
|
| 27 |
ax.set_ylabel("Bit (0 = Blue, 1 = Orange)", fontsize=10)
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
plt.tight_layout()
|
| 33 |
|
| 34 |
# Save figure to buffer
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
+
from matplotlib.patches import Patch # for legend
|
| 6 |
|
| 7 |
# ---------- Function to plot bit distribution ----------
|
| 8 |
def plot_key_bits(key_str):
|
|
|
|
| 15 |
diff = abs(zero_count - one_count)
|
| 16 |
total_bits = len(bits)
|
| 17 |
|
| 18 |
+
# Set fixed height = 1 for all bars (so 0s are visible!)
|
| 19 |
+
heights = [1] * total_bits
|
| 20 |
|
| 21 |
# Color code bits: Blue for 0, Orange for 1
|
| 22 |
colors = ['#1f77b4' if b == 0 else '#ff7f0e' for b in bits]
|
| 23 |
+
|
| 24 |
+
# Create figure
|
| 25 |
+
fig, ax = plt.subplots(figsize=(12, 2.5))
|
| 26 |
+
|
| 27 |
+
# Plot bar chart with edge line for clarity
|
| 28 |
+
ax.bar(range(total_bits), heights, color=colors, edgecolor='black', linewidth=0.2)
|
| 29 |
|
| 30 |
# Add user-friendly titles
|
| 31 |
ax.set_title("🔍 Visual Check: 0s vs 1s in Your QKD Key", fontsize=12)
|
| 32 |
ax.set_xlabel("Bit Position in Key", fontsize=10)
|
| 33 |
ax.set_ylabel("Bit (0 = Blue, 1 = Orange)", fontsize=10)
|
| 34 |
+
ax.set_yticks([]) # remove y ticks since height is always 1
|
| 35 |
|
| 36 |
+
# Add legend for 0 and 1 colors
|
| 37 |
+
legend_handles = [
|
| 38 |
+
Patch(color='#1f77b4', label='0 (Blue)'),
|
| 39 |
+
Patch(color='#ff7f0e', label='1 (Orange)')
|
| 40 |
+
]
|
| 41 |
+
ax.legend(handles=legend_handles, loc='upper right')
|
| 42 |
|
| 43 |
+
# Final layout adjustment
|
| 44 |
plt.tight_layout()
|
| 45 |
|
| 46 |
# Save figure to buffer
|