Spaces:
Sleeping
Sleeping
Embedding Explorer: add vector lines from origin + 3D grid axes
Browse files- Words shown as vectors (lines from origin to point), not just dots
- Subtle purple gridlines and zero-lines for 3D depth perception
- Aspect mode set to cube for consistent proportions
- Vector lines in both Explore and Arithmetic tabs
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -116,15 +116,31 @@ def project_3d(pca, vectors):
|
|
| 116 |
return coords
|
| 117 |
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def layout_3d(height=560):
|
| 120 |
"""Shared Plotly 3D layout."""
|
| 121 |
return dict(
|
| 122 |
scene=dict(
|
| 123 |
-
xaxis=
|
| 124 |
-
yaxis=
|
| 125 |
-
zaxis=
|
| 126 |
bgcolor=BG,
|
| 127 |
camera=dict(eye=dict(x=1.5, y=1.5, z=1.2)),
|
|
|
|
| 128 |
),
|
| 129 |
paper_bgcolor="white",
|
| 130 |
margin=dict(l=0, r=0, t=10, b=10),
|
|
@@ -139,6 +155,26 @@ def layout_3d(height=560):
|
|
| 139 |
)
|
| 140 |
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def blank(msg):
|
| 143 |
"""Empty placeholder figure with a centered message."""
|
| 144 |
fig = go.Figure()
|
|
@@ -197,7 +233,10 @@ def explore(words_text, selected):
|
|
| 197 |
# ββ Build figure ββ
|
| 198 |
fig = go.Figure()
|
| 199 |
|
| 200 |
-
#
|
|
|
|
|
|
|
|
|
|
| 201 |
fig.add_trace(go.Scatter3d(
|
| 202 |
x=main_coords[:, 0].tolist(),
|
| 203 |
y=main_coords[:, 1].tolist(),
|
|
@@ -321,6 +360,28 @@ def arithmetic(expression):
|
|
| 321 |
# ββ Build figure ββ
|
| 322 |
fig = go.Figure()
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
# Positive operands
|
| 325 |
pi = [i for i, w in enumerate(operands) if w in pos]
|
| 326 |
if pi:
|
|
|
|
| 116 |
return coords
|
| 117 |
|
| 118 |
|
| 119 |
+
def _axis(title=""):
|
| 120 |
+
"""Subtle 3D axis with gridlines for depth perception."""
|
| 121 |
+
return dict(
|
| 122 |
+
showgrid=True,
|
| 123 |
+
gridcolor="rgba(99,52,141,0.08)", # very faint purple grid
|
| 124 |
+
zeroline=True,
|
| 125 |
+
zerolinecolor="rgba(99,52,141,0.25)",
|
| 126 |
+
zerolinewidth=2,
|
| 127 |
+
showticklabels=False,
|
| 128 |
+
title=title,
|
| 129 |
+
showspikes=False,
|
| 130 |
+
backgroundcolor=BG,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
def layout_3d(height=560):
|
| 135 |
"""Shared Plotly 3D layout."""
|
| 136 |
return dict(
|
| 137 |
scene=dict(
|
| 138 |
+
xaxis=_axis(),
|
| 139 |
+
yaxis=_axis(),
|
| 140 |
+
zaxis=_axis(),
|
| 141 |
bgcolor=BG,
|
| 142 |
camera=dict(eye=dict(x=1.5, y=1.5, z=1.2)),
|
| 143 |
+
aspectmode="cube",
|
| 144 |
),
|
| 145 |
paper_bgcolor="white",
|
| 146 |
margin=dict(l=0, r=0, t=10, b=10),
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
|
| 158 |
+
def add_vectors_from_origin(fig, coords, labels, color=PURPLE, width=3):
|
| 159 |
+
"""Draw vector lines from origin to each point (words ARE vectors)."""
|
| 160 |
+
for i, label in enumerate(labels):
|
| 161 |
+
fig.add_trace(go.Scatter3d(
|
| 162 |
+
x=[0, coords[i, 0]],
|
| 163 |
+
y=[0, coords[i, 1]],
|
| 164 |
+
z=[0, coords[i, 2]],
|
| 165 |
+
mode="lines",
|
| 166 |
+
line=dict(color=color, width=width),
|
| 167 |
+
showlegend=False, hoverinfo="none",
|
| 168 |
+
))
|
| 169 |
+
# Origin marker
|
| 170 |
+
fig.add_trace(go.Scatter3d(
|
| 171 |
+
x=[0], y=[0], z=[0],
|
| 172 |
+
mode="markers",
|
| 173 |
+
marker=dict(size=4, color=GRAY, opacity=0.5),
|
| 174 |
+
name="Origin", hoverinfo="text", hovertext=["Origin (0,0,0)"],
|
| 175 |
+
))
|
| 176 |
+
|
| 177 |
+
|
| 178 |
def blank(msg):
|
| 179 |
"""Empty placeholder figure with a centered message."""
|
| 180 |
fig = go.Figure()
|
|
|
|
| 233 |
# ββ Build figure ββ
|
| 234 |
fig = go.Figure()
|
| 235 |
|
| 236 |
+
# Vector lines from origin to each word
|
| 237 |
+
add_vectors_from_origin(fig, main_coords, valid)
|
| 238 |
+
|
| 239 |
+
# Main words (markers + labels at tips of vectors)
|
| 240 |
fig.add_trace(go.Scatter3d(
|
| 241 |
x=main_coords[:, 0].tolist(),
|
| 242 |
y=main_coords[:, 1].tolist(),
|
|
|
|
| 360 |
# ββ Build figure ββ
|
| 361 |
fig = go.Figure()
|
| 362 |
|
| 363 |
+
# Vector lines from origin for all operands
|
| 364 |
+
for i, w in enumerate(operands):
|
| 365 |
+
color = PURPLE if w in pos else PINK
|
| 366 |
+
fig.add_trace(go.Scatter3d(
|
| 367 |
+
x=[0, coords[i, 0]], y=[0, coords[i, 1]], z=[0, coords[i, 2]],
|
| 368 |
+
mode="lines", line=dict(color=color, width=3),
|
| 369 |
+
showlegend=False, hoverinfo="none",
|
| 370 |
+
))
|
| 371 |
+
# Vector line for top result
|
| 372 |
+
if result_words:
|
| 373 |
+
fig.add_trace(go.Scatter3d(
|
| 374 |
+
x=[0, coords[n_op, 0]], y=[0, coords[n_op, 1]], z=[0, coords[n_op, 2]],
|
| 375 |
+
mode="lines", line=dict(color=GOLD, width=3),
|
| 376 |
+
showlegend=False, hoverinfo="none",
|
| 377 |
+
))
|
| 378 |
+
# Origin
|
| 379 |
+
fig.add_trace(go.Scatter3d(
|
| 380 |
+
x=[0], y=[0], z=[0], mode="markers",
|
| 381 |
+
marker=dict(size=4, color=GRAY, opacity=0.5),
|
| 382 |
+
name="Origin", hoverinfo="text", hovertext=["Origin"],
|
| 383 |
+
))
|
| 384 |
+
|
| 385 |
# Positive operands
|
| 386 |
pi = [i for i, w in enumerate(operands) if w in pos]
|
| 387 |
if pi:
|