plotting
Trajectory visualization and plotting utilities.
Requires the plotting extra::
pip install 'openscvx[plotting]'
This module provides reusable building blocks for visualizing trajectory optimization results. It is intentionally minimal - we provide common utilities that can be composed together, not a complete solution that tries to do everything for you.
2D Plots (plotly-based): Two-layer API for time series visualization::
from openscvx.plotting import plot_states, plot_controls, plot_vector_norm
# High-level: subplot grid with individual scaling per component
plot_states(results, ["position", "velocity"]).show()
plot_controls(results, ["thrust"]).show()
# Low-level: single component
plot_state_component(results, "position", component=2).show() # z only
# Specialized plots
plot_vector_norm(results, "thrust", bounds=(rho_min, rho_max)).show()
plot_projections_2d(results, velocity_var_name="velocity").show()
Publication styling:
plot_states, plot_controls, and plot_scp_iterations accept
style="publication" (white) or style="publication_dark". Figures you
assemble yourself get the same look from apply_publication_plotly_layout
for Plotly and apply_latin_modern_to_axis for matplotlib, with the
palettes from publication_trace_colors / publication_dark_colors.
3D Visualization (viser-based):
The viser submodule provides composable primitives for building
interactive 3D visualizations. See openscvx.plotting.viser for details::
from openscvx.plotting import viser
server = viser.create_server(positions)
viser.add_gates(server, gate_vertices)
server.sleep_forever()
For problem-specific visualization examples (drones, rockets, etc.), see
examples/plotting_viser.py.
PublicationFigure
¶
Plotly figure wrapper with Latin Modern show() and matplotlib save_pdf().
Source code in openscvx/plotting/publication.py
apply_latin_modern_to_axis(ax, lm_fp) -> None
¶
Restyle a matplotlib axis' ticks, labels, and title in Latin Modern.
Matplotlib applies a font at draw time per artist, so text created before
the font was chosen keeps the default; call this once an axis is fully
populated. lm_fp is a :func:latin_modern_fontproperties result, and
None (font not installed) leaves the axis untouched.
Source code in openscvx/plotting/publication.py
apply_publication_plotly_layout(fig: go.Figure, *, n_rows: int = 1, n_cols: int = 1, extra_legend_width: int = 0, dark: bool = False, width: int | None = None, height: int | None = None) -> None
¶
Apply Latin Modern fonts, LaTeX-friendly defaults, and fixed size to a figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Figure to restyle in place. |
required |
n_rows
|
int
|
Rows in the subplot grid, used to size the figure. |
1
|
n_cols
|
int
|
Columns in the subplot grid, used to size the figure. |
1
|
extra_legend_width
|
int
|
Pixels to add for a legend outside the panels. |
0
|
dark
|
bool
|
Use the dark palette (:func: |
False
|
width
|
int | None
|
Overrides the grid-derived width, in pixels. Pass it for figures whose aspect ratio is set by the data rather than by the panel grid, such as an equal-axis planar trajectory. |
None
|
height
|
int | None
|
Overrides the grid-derived height, in pixels. |
None
|
Source code in openscvx/plotting/publication.py
latin_modern_fontproperties()
¶
Matplotlib FontProperties for Latin Modern Roman (PDF export).
Source code in openscvx/plotting/publication.py
plot_control_component(result: OptimizationResults, control_name: str, component: int = 0) -> go.Figure
¶
Plot a single component of a control variable vs time.
This is the low-level function for plotting one scalar control over time. For plotting all components of a control, use plot_controls().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing control trajectories |
required |
control_name
|
str
|
Name of the control variable |
required |
component
|
int
|
Component index (0-indexed). For scalar controls, use 0. |
0
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure with single plot |
Example
plot_control_component(result, "thrust", 0) # Plot thrust_x
Source code in openscvx/plotting/plotting.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
plot_controls(result: OptimizationResults, control_names: list[VarSpec] | None = None, include_private: bool = False, cols: int = 3, style: PlotStyle = 'dark', pdf_path: str | Path | None = None) -> go.Figure
¶
Plot control variables in a subplot grid.
Each component of each control gets its own subplot with individual y-axis scaling. This is the primary function for visualizing control trajectories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing control trajectories |
required |
control_names
|
list[VarSpec] | None
|
List of control names to plot. If None, plots all controls.
For multidimensional controls, pass a single component with
|
None
|
include_private
|
bool
|
Whether to include private controls (names starting with '_') |
False
|
cols
|
int
|
Maximum number of columns in subplot grid |
3
|
style
|
PlotStyle
|
|
'dark'
|
pdf_path
|
str | Path | None
|
Output path for the PDF when |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure, or a :class: |
Figure
|
wrapper when |
Examples:
>>> plot_controls(result, ["thrust_force"]) # 3 subplots for x, y, z
>>> plot_controls(result, ["thrust_force:2"]) # fz only
>>> plot_controls(result, style="publication")
Source code in openscvx/plotting/plotting.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
plot_projections_2d(result: OptimizationResults, var_name: str = 'position', velocity_var_name: str | None = None, cmap: str = 'viridis') -> go.Figure
¶
Plot XY, XZ, YZ projections of a 3D variable.
Useful for visualizing 3D trajectories in 2D plane views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing trajectories |
required |
var_name
|
str
|
Name of the 3D variable to plot (default: "position") |
'position'
|
velocity_var_name
|
str | None
|
Optional name of velocity variable for coloring by speed. If provided, trajectory points are colored by velocity magnitude. |
None
|
cmap
|
str
|
Matplotlib colormap name for velocity coloring (default: "viridis") |
'viridis'
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure with three subplots (XY, XZ, YZ planes) |
Source code in openscvx/plotting/plotting.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
plot_scp_convergence_histories(result: OptimizationResults) -> go.Figure
¶
Plot SCP convergence histories: trust region weight, reduction histories, and acceptance ratio.
Creates three separate plots: 1. Trust region weight (lam_prox) history 2. Actual and predicted reduction histories (overlaid) 3. Acceptance ratio history
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
OptimizationResults containing the convergence histories. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure with three subplots |
Example
problem.initialize() result = problem.solve() plot_scp_convergence_histories(result).show()
Source code in openscvx/plotting/scp_iteration.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | |
plot_scp_iterations(result: OptimizationResults, state_names: list[VarSpec] | None = None, control_names: list[VarSpec] | None = None, cmap_name: str = 'viridis', show_propagation: bool = True, style: PlotStyle = 'dark', pdf_path: str | Path | None = None) -> go.Figure
¶
Plot all SCP iterations overlaid with colormap-based coloring.
Shows the evolution of states and controls across SCP iterations. Early iterations are dark, later iterations are bright (following the colormap). This makes convergence visible at a glance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing iteration history |
required |
state_names
|
list[VarSpec] | None
|
Optional list of state names to include. If None, plots all states.
For multidimensional states, pass a single component with
|
None
|
control_names
|
list[VarSpec] | None
|
Optional list of control names to include. If None, plots all controls.
Component selection uses the same syntax as |
None
|
cmap_name
|
str
|
Matplotlib colormap name (default: "viridis") |
'viridis'
|
show_propagation
|
bool
|
If True, show multi-shot propagation lines (default: True) |
True
|
style
|
PlotStyle
|
|
'dark'
|
pdf_path
|
str | Path | None
|
Output path for the PDF when |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure, or a :class: |
Figure
|
wrapper when |
Example
results = problem.solve() plot_scp_iterations(results, ["position", "velocity"]).show() plot_scp_iterations(results, ["position:0"], style="publication").show()
Source code in openscvx/plotting/scp_iteration.py
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 147 148 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 242 243 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
plot_state_component(result: OptimizationResults, state_name: str, component: int = 0) -> go.Figure
¶
Plot a single component of a state variable vs time.
This is the low-level function for plotting one scalar value over time. For plotting all components of a state, use plot_states().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing state trajectories |
required |
state_name
|
str
|
Name of the state variable |
required |
component
|
int
|
Component index (0-indexed). For scalar states, use 0. |
0
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure with single plot |
Example
plot_state_component(result, "position", 2) # Plot z-component
Source code in openscvx/plotting/plotting.py
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 242 243 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 | |
plot_states(result: OptimizationResults, state_names: list[VarSpec] | None = None, include_private: bool = False, cols: int = 4, style: PlotStyle = 'dark', pdf_path: str | Path | None = None) -> go.Figure
¶
Plot state variables in a subplot grid.
Each component of each state gets its own subplot with individual y-axis scaling. This is the primary function for visualizing state trajectories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing state trajectories |
required |
state_names
|
list[VarSpec] | None
|
List of state names to plot. If None, plots all states.
For multidimensional states, pass a single component with
|
None
|
include_private
|
bool
|
Whether to include private states (names starting with '_') |
False
|
cols
|
int
|
Maximum number of columns in subplot grid |
4
|
style
|
PlotStyle
|
|
'dark'
|
pdf_path
|
str | Path | None
|
Output path for the PDF when |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure, or a :class: |
Figure
|
wrapper when |
Examples:
>>> plot_states(result, ["position"]) # 3 subplots for x, y, z
>>> plot_states(result, ["position:2"]) # z component only
>>> plot_states(result, [("position", 0), "velocity"]) # x and all velocity
>>> plot_states(result, style="publication", pdf_path="figures/states.pdf")
Source code in openscvx/plotting/plotting.py
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
plot_trust_region_heatmap(result: OptimizationResults)
¶
Plot heatmap of the final trust-region deltas (TR_history[-1]).
Source code in openscvx/plotting/plotting.py
plot_vector_norm(result: OptimizationResults, var_name: str, bounds: tuple[float, float] | None = None) -> go.Figure
¶
Plot the 2-norm of a vector variable over time.
Useful for visualizing thrust magnitude, velocity magnitude, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResults
|
Optimization results containing trajectories |
required |
var_name
|
str
|
Name of the vector variable (state or control) |
required |
bounds
|
tuple[float, float] | None
|
Optional (min, max) bounds to show as horizontal dashed lines |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure |
Source code in openscvx/plotting/plotting.py
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
plot_virtual_control_heatmap(result: OptimizationResults)
¶
Plot heatmap of the final virtual control magnitudes (VC_history[-1]).
Source code in openscvx/plotting/plotting.py
publication_dark_colors() -> dict[str, str]
¶
Surround colors (background, grid, foreground) for "publication_dark".
Figures assembled by hand — matplotlib PDF exports especially — should read
the palette from here rather than respell the hex values, so a dark figure
matches one produced by style="publication_dark".
Source code in openscvx/plotting/publication.py
publication_trace_colors() -> dict[str, str]
¶
Trace colors (trajectory, nodes, bounds, impulses) shared by both publication styles.
show_plotly_with_latin_modern(fig: go.Figure) -> None
¶
Open a Plotly figure in the browser with Latin Modern for axes and legend.