Skip to content

plotting

Plotting helpers for hybrid system traces.

plot_trace(trace, dims=None, *, location_colors=None, show_locations=True, show_location_labels=False, show_events=True, show_event_labels=True, show=False, ax=None)

Plot state trajectories with location shading and event markers.

Parameters:

Name Type Description Default
trace Trace

Trace to visualize.

required
dims Sequence[int] | None

State indices to plot.

None
location_colors Mapping[str, str] | None

Optional color mapping for locations.

None
show_locations bool

Whether to shade location regions.

True
show_location_labels bool

Whether to label locations above the trace.

False
show_events bool

Whether to show transition events.

True
show_event_labels bool

Whether to label transition events.

True
show bool

Whether to call matplotlib show().

False
ax Axes | None

Optional axis to draw into.

None

Returns:

Type Description
Axes

Matplotlib axes containing the plot.

Source code in src/flowcean/ode/plotting.py
13
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
def plot_trace(
    trace: Trace,
    dims: Sequence[int] | None = None,
    *,
    location_colors: Mapping[str, str] | None = None,
    show_locations: bool = True,
    show_location_labels: bool = False,
    show_events: bool = True,
    show_event_labels: bool = True,
    show: bool = False,
    ax: Axes | None = None,
) -> Axes:
    """Plot state trajectories with location shading and event markers.

    Args:
        trace: Trace to visualize.
        dims: State indices to plot.
        location_colors: Optional color mapping for locations.
        show_locations: Whether to shade location regions.
        show_location_labels: Whether to label locations above the trace.
        show_events: Whether to show transition events.
        show_event_labels: Whether to label transition events.
        show: Whether to call matplotlib show().
        ax: Optional axis to draw into.

    Returns:
        Matplotlib axes containing the plot.
    """
    if dims is None:
        dims = list(range(trace.x.shape[1]))

    if ax is None:
        _, ax = plt.subplots()
    if ax is None:
        message = "Failed to create matplotlib axes."
        raise RuntimeError(message)

    for dim in dims:
        ax.plot(trace.t, trace.x[:, dim], label=f"x{dim}")

    if show_locations:
        _plot_location_spans(
            trace,
            ax=ax,
            location_colors=location_colors,
            show_labels=show_location_labels,
        )

    if show_events:
        _plot_events(trace, ax=ax, show_labels=show_event_labels)

    ax.set_xlabel("t")
    ax.set_ylabel("state")
    ax.legend(loc="best")

    if show:
        plt.show()

    return ax

plot_phase(trace, x_dim=0, y_dim=1, *, location_colors=None, show_location_legend=True, show=False, ax=None)

Plot phase portrait segments colored by location.

Parameters:

Name Type Description Default
trace Trace

Trace to visualize.

required
x_dim int

State index on the x-axis.

0
y_dim int

State index on the y-axis.

1
location_colors Mapping[str, str] | None

Optional color mapping for locations.

None
show_location_legend bool

Whether to show location labels in legend.

True
show bool

Whether to call matplotlib show().

False
ax Axes | None

Optional axis to draw into.

None

Returns:

Type Description
Axes

Matplotlib axes containing the plot.

Source code in src/flowcean/ode/plotting.py
 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
def plot_phase(
    trace: Trace,
    x_dim: int = 0,
    y_dim: int = 1,
    *,
    location_colors: Mapping[str, str] | None = None,
    show_location_legend: bool = True,
    show: bool = False,
    ax: Axes | None = None,
) -> Axes:
    """Plot phase portrait segments colored by location.

    Args:
        trace: Trace to visualize.
        x_dim: State index on the x-axis.
        y_dim: State index on the y-axis.
        location_colors: Optional color mapping for locations.
        show_location_legend: Whether to show location labels in legend.
        show: Whether to call matplotlib show().
        ax: Optional axis to draw into.

    Returns:
        Matplotlib axes containing the plot.
    """
    if ax is None:
        _, ax = plt.subplots()
    if ax is None:
        message = "Failed to create matplotlib axes."
        raise RuntimeError(message)

    segments, locations = _location_segments(trace)
    colors = _location_color_map(locations, location_colors)

    for start, end, location in segments:
        ax.plot(
            trace.x[start:end, x_dim],
            trace.x[start:end, y_dim],
            color=colors[location],
            label=f"{location}",
        )
    ax.set_xlabel(f"x{x_dim}")
    ax.set_ylabel(f"x{y_dim}")

    if show_location_legend:
        _dedupe_legend(ax)

    if show:
        plt.show()

    return ax