plotly_integration
Plotly integration for viser - animated 2D plots synchronized with 3D visualization.
This module provides utilities for embedding plotly figures in viser's GUI with animated markers that synchronize with the 3D animation timeline.
add_animated_plotly_marker(server: viser.ViserServer, fig: go.Figure, time_array: np.ndarray, marker_x_data: np.ndarray, marker_y_data: np.ndarray, use_trajectory_indexing: bool = True, marker_name: str = 'Current', marker_color: str = 'red', marker_size: int = 12, folder_name: str | None = None, aspect: float = 1.5) -> tuple
¶
Add a plotly figure to viser GUI with an animated time marker.
This function takes any plotly figure and adds an animated marker that synchronizes with viser's 3D animation timeline. The marker shows the current position on the plot as the animation plays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
ViserServer
|
ViserServer instance |
required |
fig
|
Figure
|
Plotly figure to display |
required |
time_array
|
ndarray
|
Time values corresponding to animation frames (N,). This should match the time array passed to add_animation_controls(). |
required |
marker_x_data
|
ndarray
|
X-axis values for marker position (N,) |
required |
marker_y_data
|
ndarray
|
Y-axis values for marker position (N,) |
required |
use_trajectory_indexing
|
bool
|
If True, frame_idx maps directly to data indices. If False, searches for nearest time value (use for node-only data). |
True
|
marker_name
|
str
|
Legend name for the marker trace |
'Current'
|
marker_color
|
str
|
Color of the animated marker |
'red'
|
marker_size
|
int
|
Size of the animated marker in points |
12
|
folder_name
|
str | None
|
Optional GUI folder name to organize plots |
None
|
aspect
|
float
|
Aspect ratio for plot display (width/height) |
1.5
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (plot_handle, update_callback) |
Example::
from openscvx.plotting import plot_vector_norm, viser
# Create any plotly figure
fig = plot_vector_norm(results, "thrust")
thrust_norms = np.linalg.norm(results.trajectory["thrust"], axis=1)
# Add to viser with animated marker
_, update_plot = viser.add_animated_plotly_marker(
server, fig,
time_array=results.trajectory["time"].flatten(),
marker_x_data=results.trajectory["time"].flatten(),
marker_y_data=thrust_norms,
)
# Add to animation callbacks
update_callbacks.append(update_plot)
Source code in openscvx/plotting/viser/plotly_integration.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
add_animated_plotly_vline(server: viser.ViserServer, fig: go.Figure, time_array: np.ndarray, use_trajectory_indexing: bool = True, line_color: str = 'red', line_width: int = 2, line_dash: str = 'dash', annotation_text: str = 'Current', annotation_position: str = 'top', folder_name: str | None = None, aspect: float = 1.5) -> tuple
¶
Add a plotly figure to viser GUI with an animated vertical line.
This function takes any plotly figure and adds an animated vertical line that synchronizes with viser's 3D animation timeline. The line shows the current time position as the animation plays.
This is more generic than add_animated_plotly_marker() as it works for any number of traces without needing to specify y-data for each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
ViserServer
|
ViserServer instance |
required |
fig
|
Figure
|
Plotly figure to display |
required |
time_array
|
ndarray
|
Time values corresponding to animation frames (N,). This should match the time array passed to add_animation_controls(). |
required |
use_trajectory_indexing
|
bool
|
If True, frame_idx maps directly to time indices. If False, searches for nearest time value (use for node-only data). |
True
|
line_color
|
str
|
Color of the vertical line |
'red'
|
line_width
|
int
|
Width of the vertical line in pixels |
2
|
line_dash
|
str
|
Dash style - "solid", "dash", "dot", "dashdot" |
'dash'
|
annotation_text
|
str
|
Text to show on the line |
'Current'
|
annotation_position
|
str
|
Position of annotation - "top", "bottom", "top left", etc. |
'top'
|
folder_name
|
str | None
|
Optional GUI folder name to organize plots |
None
|
aspect
|
float
|
Aspect ratio for plot display (width/height) |
1.5
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (plot_handle, update_callback) |
Example::
from openscvx.plotting import plot_control, viser
# Create any plotly figure
fig = plot_control(results, "thrust_force")
# Add to viser with animated vertical line
_, update_plot = viser.add_animated_plotly_vline(
server, fig,
time_array=results.trajectory["time"].flatten(),
)
# Add to animation callbacks
update_callbacks.append(update_plot)
Source code in openscvx/plotting/viser/plotly_integration.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
add_animated_vector_norm_plot(server: viser.ViserServer, results: OptimizationResults, var_name: str, bounds: tuple[float, float] | None = None, title: str | None = None, folder_name: str | None = None, aspect: float = 1.5, marker_color: str = 'red', marker_size: int = 12) -> tuple
¶
Add animated norm plot for a state or control variable.
Convenience wrapper around add_animated_plotly_marker() that uses the existing plot_vector_norm() function to create the base plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
ViserServer
|
ViserServer instance |
required |
results
|
OptimizationResults
|
Optimization results containing variable data |
required |
var_name
|
str
|
Name of the state or control variable to plot |
required |
bounds
|
tuple[float, float] | None
|
Optional (min, max) bounds to display on plot |
None
|
title
|
str | None
|
Optional custom title for the plot (defaults to "‖{var_name}‖₂") |
None
|
folder_name
|
str | None
|
Optional GUI folder name to organize plots |
None
|
aspect
|
float
|
Aspect ratio for plot display (width/height) |
1.5
|
marker_color
|
str
|
Color of the animated marker |
'red'
|
marker_size
|
int
|
Size of the animated marker in points |
12
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (plot_handle, update_callback), or (None, None) if variable not found |
Example::
from openscvx.plotting import viser
# Add animated thrust norm plot
_, update_thrust = viser.add_animated_vector_norm_plot(
server, results, "thrust",
title="Thrust Magnitude",
bounds=(0.0, max_thrust),
folder_name="Control Plots"
)
if update_thrust is not None:
update_callbacks.append(update_thrust)
Source code in openscvx/plotting/viser/plotly_integration.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |