pylinkage.visualizer package

Submodules

pylinkage.visualizer.animated module

The visualizer module makes visualization of linkages easy using matplotlib.

Created on Mon Jun 14, 12:13:58 2021.

@author: HugoFara

pylinkage.visualizer.animated.plot_kinematic_linkage(linkage: Linkage, fig: Figure, axis: Axes, loci: Sequence[tuple[Coord, ...]], frames: int = 100, interval: float = 40) FuncAnimation

Plot a linkage with an animation.

Args:

linkage: The linkage to animate. fig: Figure to support the axes. axis: The subplot to draw on. loci: list of list of coordinates. frames: Number of frames to draw the linkage on. interval: Delay between frames in milliseconds.

Returns:

The animation object.

pylinkage.visualizer.animated.show_linkage(linkage: Linkage, save: bool = False, prev: Sequence[Coord] | None = None, loci: Sequence[tuple[Coord, ...]] | None = None, points: int = 100, iteration_factor: float = 1, title: str | None = None, duration: float = 5, fps: int = 24) FuncAnimation

Display results as an animated drawing.

Args:

linkage: The Linkage you want to draw. save: To save the animation. prev: Previous coordinates to use for linkage. loci: list of loci. points: Number of points to draw for a crank revolution.

Useless when loci are set.

iteration_factor: A simple way to subdivide the movement. The real

number of points will be points * iteration_factor.

title: Figure title. Defaults to str(len(ani)). duration: Animation duration (in seconds). fps: Number of frames per second for the output video.

Returns:

The animation object.

pylinkage.visualizer.animated.swarm_tiled_repr(linkage: Linkage, swarm: tuple[int, Sequence[tuple[float, np.ndarray, Sequence[Coord]]]], fig: Figure, axes: np.ndarray, dimension_func: Callable[[np.ndarray], Sequence[float]] | None = None, points: int = 12, iteration_factor: float = 1) None

Show all the linkages in a swarm in tiled mode.

Args:

linkage: The original Linkage that will be MODIFIED. swarm: Tuple of (iteration_number, list_of_agents) where each agent is

(score, dimensions, initial_positions).

fig: Figure to support the axes. axes: The subplot to draw on. points: Number of steps to use for each Linkage. iteration_factor: A simple way to subdivide the movement. The real

number of points will be points * iteration_factor.

dimension_func: If you want a special formatting of dimensions from

agents before passing them to the linkage.

pylinkage.visualizer.animated.update_animated_plot(linkage: Linkage, index: int, images: list[Line2D], loci: Sequence[tuple[Coord, ...]]) list[Line2D]

Modify im, instead of recreating it to make the animation run faster.

Args:

linkage: The linkage being animated. index: Frame index. images: Artist to be modified. loci: list of loci.

Returns:

Updated version of images.

pylinkage.visualizer.core module

Core features for visualization.

This module provides shared utilities for matplotlib-based visualization. For symbol definitions used by other backends (Plotly, drawsvg), see symbols.py.

pylinkage.visualizer.static module

Static (not animated) visualization.

pylinkage.visualizer.static.plot_static_linkage(linkage: Linkage, axis: Axes, loci: Iterable[tuple[Coord, ...]], locus_highlights: list[list[Coord]] | None = None, show_legend: bool = False) None

Plot a linkage without movement.

Args:

linkage: The linkage you want to see. axis: The graph we should draw on. loci: List of list of coordinates. They will be plotted. locus_highlights: If a list, should be a list of list of coordinates you

want to see highlighted.

show_legend: To add an automatic legend to the graph.

Module contents

Linkage visualization features.

Backends:
  • matplotlib (default): plot_static_linkage, plot_kinematic_linkage, show_linkage

  • plotly: plot_linkage_plotly, animate_linkage_plotly (interactive HTML)

  • drawsvg: plot_linkage_svg, save_linkage_svg (publication-quality SVG)

class pylinkage.visualizer.LinkStyle(value)

Bases: Enum

Visual styles for drawing links between joints.

BAR = 1
BONE = 2
LINE = 3
class pylinkage.visualizer.SymbolType(value)

Bases: Enum

Types of kinematic symbols.

CRANK = 3
FIXED = 5
GROUND = 1
LINEAR = 6
REVOLUTE = 2
SLIDER = 4
pylinkage.visualizer.animate_dashboard(linkage: Linkage, history: History, dim_names: Sequence[str], dim_types: Sequence[str] | None = None, bounds: tuple[Sequence[float], Sequence[float]] | None = None, dimension_func: Callable[[np.ndarray], Sequence[float]] | None = None, interval: int = 500, save_path: str | None = None) FuncAnimation

Animate the dashboard layout over optimization history.

Args:

linkage: The linkage being optimized. history: List of swarms, one per iteration. dim_names: Names for each dimension. dim_types: Type of each dimension (‘length’ or ‘angle’). bounds: Optional (min_bounds, max_bounds). dimension_func: Optional function to transform dimensions. interval: Delay between frames in milliseconds. save_path: If provided, save animation to this path.

Returns:

The animation object.

pylinkage.visualizer.animate_linkage_plotly(linkage: Linkage, loci: Iterable[tuple[Coord, ...]] | None = None, *, title: str | None = None, show_loci: bool = True, width: int = 900, height: int = 700, frame_duration: int = 50) Figure

Create an animated Plotly diagram with play/pause controls.

Args:

linkage: The linkage to visualize. loci: Optional precomputed loci. If None, runs simulation. title: Optional title for the diagram. show_loci: Whether to show joint movement paths. width: Figure width in pixels. height: Figure height in pixels. frame_duration: Milliseconds per frame.

Returns:

A plotly Figure object with animation.

pylinkage.visualizer.animate_parallel_coordinates(history: History, dim_names: Sequence[str], dim_types: Sequence[str] | None = None, bounds: tuple[Sequence[float], Sequence[float]] | None = None, interval: int = 200, cmap: str = 'viridis', save_path: str | None = None) FuncAnimation

Animate parallel coordinates plot over optimization history.

Args:

history: List of swarms, one per iteration. dim_names: Names for each dimension. dim_types: Type of each dimension (‘length’, ‘angle’, or ‘score’). bounds: Optional (min_bounds, max_bounds) for normalization. interval: Delay between frames in milliseconds. cmap: Colormap name. save_path: If provided, save animation to this path.

Returns:

The animation object.

pylinkage.visualizer.dashboard_layout(linkage: Linkage, swarm: Swarm, score_history: Sequence[float], dim_names: Sequence[str], dim_types: Sequence[str] | None = None, bounds: tuple[Sequence[float], Sequence[float]] | None = None, dimension_func: Callable[[np.ndarray], Sequence[float]] | None = None, fig: Figure | None = None) Figure

Create a dashboard layout for PSO visualization.

Layout: +—————–+——————+ | Score History | Best Linkage | | (line plot) | (static plot) | +—————–+——————+ | Length Params | Angle Params | | (box plot) | (polar/circular) | +—————–+——————+

Args:

linkage: The linkage being optimized (will be modified). swarm: Current swarm state. score_history: History of best scores per iteration. dim_names: Names for each dimension. dim_types: Type of each dimension (‘length’ or ‘angle’). bounds: Optional (min_bounds, max_bounds). dimension_func: Optional function to transform dimensions. fig: Existing figure to use.

Returns:

The matplotlib Figure object.

pylinkage.visualizer.parallel_coordinates_plot(swarm: Swarm, dim_names: Sequence[str], dim_types: Sequence[str] | None = None, bounds: tuple[Sequence[float], Sequence[float]] | None = None, ax: Axes | None = None, cbar_ax: Axes | None = None, cmap: str = 'viridis', alpha: float = 0.3, highlight_best: int = 5) Axes

Create a parallel coordinates plot for a PSO swarm.

Each dimension gets its own vertical axis, normalized to [0, 1]. Particles are colored by their score (fitness).

Args:
swarm: Tuple of (iteration, list_of_agents) where each agent is

(score, dimensions, initial_positions).

dim_names: Names for each dimension. dim_types: Type of each dimension (‘length’, ‘angle’, or ‘score’).

Used for axis grouping and labeling.

bounds: Optional (min_bounds, max_bounds) for normalization. ax: Matplotlib axes to plot on. If None, creates new figure. cbar_ax: Optional axes for the colorbar. If provided, colorbar is

drawn there instead of stealing space from ax. Use this for animations to prevent layout shifts.

cmap: Colormap name for score coloring. alpha: Line transparency for regular particles. highlight_best: Number of best particles to highlight.

Returns:

The matplotlib Axes object.

pylinkage.visualizer.plot_kinematic_linkage(linkage: Linkage, fig: Figure, axis: Axes, loci: Sequence[tuple[Coord, ...]], frames: int = 100, interval: float = 40) FuncAnimation

Plot a linkage with an animation.

Args:

linkage: The linkage to animate. fig: Figure to support the axes. axis: The subplot to draw on. loci: list of list of coordinates. frames: Number of frames to draw the linkage on. interval: Delay between frames in milliseconds.

Returns:

The animation object.

pylinkage.visualizer.plot_linkage_plotly(linkage: Linkage, loci: Iterable[tuple[Coord, ...]] | None = None, *, title: str | None = None, show_dimensions: bool = False, show_loci: bool = True, show_labels: bool = True, width: int = 800, height: int = 600) Figure

Create an interactive Plotly diagram of a linkage.

Args:

linkage: The linkage to visualize. loci: Optional precomputed loci. If None, runs simulation. title: Optional title for the diagram. show_dimensions: Whether to show dimension annotations. show_loci: Whether to show joint movement paths. show_labels: Whether to show joint name labels. width: Figure width in pixels. height: Figure height in pixels.

Returns:

A plotly Figure object.

pylinkage.visualizer.plot_linkage_svg(linkage: Linkage, loci: Iterable[tuple[Coord, ...]] | None = None, *, title: str | None = None, show_dimensions: bool = False, show_loci: bool = True, show_labels: bool = True, link_style: Literal['bar', 'bone', 'line'] = 'bar', scale: float = 80, padding: float = 100) Drawing

Create a publication-quality SVG kinematic diagram.

Args:

linkage: The linkage to visualize. loci: Optional precomputed loci. If None, runs simulation. title: Optional title for the diagram. show_dimensions: Whether to show dimension lines. show_loci: Whether to show joint movement paths. show_labels: Whether to show joint labels. link_style: Visual style for links (‘bar’, ‘bone’, or ‘line’). scale: Pixels per unit. padding: Canvas padding in pixels.

Returns:

A drawsvg.Drawing object.

pylinkage.visualizer.plot_static_linkage(linkage: Linkage, axis: Axes, loci: Iterable[tuple[Coord, ...]], locus_highlights: list[list[Coord]] | None = None, show_legend: bool = False) None

Plot a linkage without movement.

Args:

linkage: The linkage you want to see. axis: The graph we should draw on. loci: List of list of coordinates. They will be plotted. locus_highlights: If a list, should be a list of list of coordinates you

want to see highlighted.

show_legend: To add an automatic legend to the graph.

pylinkage.visualizer.save_linkage_svg(linkage: Linkage, path: str, loci: Iterable[tuple[Coord, ...]] | None = None, **kwargs: object) None

Save a linkage diagram to an SVG file.

Args:

linkage: The linkage to visualize. path: Output file path (should end in .svg). loci: Optional precomputed loci. **kwargs: Additional arguments passed to plot_linkage_svg.

pylinkage.visualizer.show_linkage(linkage: Linkage, save: bool = False, prev: Sequence[Coord] | None = None, loci: Sequence[tuple[Coord, ...]] | None = None, points: int = 100, iteration_factor: float = 1, title: str | None = None, duration: float = 5, fps: int = 24) FuncAnimation

Display results as an animated drawing.

Args:

linkage: The Linkage you want to draw. save: To save the animation. prev: Previous coordinates to use for linkage. loci: list of loci. points: Number of points to draw for a crank revolution.

Useless when loci are set.

iteration_factor: A simple way to subdivide the movement. The real

number of points will be points * iteration_factor.

title: Figure title. Defaults to str(len(ani)). duration: Animation duration (in seconds). fps: Number of frames per second for the output video.

Returns:

The animation object.

pylinkage.visualizer.swarm_tiled_repr(linkage: Linkage, swarm: tuple[int, Sequence[tuple[float, np.ndarray, Sequence[Coord]]]], fig: Figure, axes: np.ndarray, dimension_func: Callable[[np.ndarray], Sequence[float]] | None = None, points: int = 12, iteration_factor: float = 1) None

Show all the linkages in a swarm in tiled mode.

Args:

linkage: The original Linkage that will be MODIFIED. swarm: Tuple of (iteration_number, list_of_agents) where each agent is

(score, dimensions, initial_positions).

fig: Figure to support the axes. axes: The subplot to draw on. points: Number of steps to use for each Linkage. iteration_factor: A simple way to subdivide the movement. The real

number of points will be points * iteration_factor.

dimension_func: If you want a special formatting of dimensions from

agents before passing them to the linkage.