Skip to content

trading.analyzer.graphing.parallel_coordinates

trading.analyzer.graphing.parallel_coordinates

build_parallel_coordinates

build_parallel_coordinates(df: DataFrame, param_columns: list[str], target_metric: str = 'calmar_ratio', sample_size: int | None = None) -> go.Figure

Builds the parallel coordinates plot across all swept parameters.

Parameters:

Name Type Description Default
df DataFrame

Experiment DataFrame.

required
param_columns list[str]

Parameter columns to include as axes.

required
target_metric str

Metric column used as the final axis and the color scale.

'calmar_ratio'
sample_size int | None

If the experiment count is large (hundreds to thousands of combos), parallel coordinates gets visually dense fast.

None

Returns:

Type Description
Figure

A plotly Figure.

Source code in src\contango\trading\analyzer\graphing\parallel_coordinates.py
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
def build_parallel_coordinates(
    df: pd.DataFrame,
    param_columns: list[str],
    target_metric: str = "calmar_ratio",
    sample_size: int | None = None,
) -> go.Figure:
    """
    Builds the parallel coordinates plot across all swept parameters.

    Args:
        df: Experiment DataFrame.
        param_columns: Parameter columns to include as axes.
        target_metric: Metric column used as the final axis and the color scale.
        sample_size: If the experiment count is large (hundreds to thousands of
                     combos), parallel coordinates gets visually dense fast.

    Returns:
        A plotly Figure.
    """
    plot_df = df.dropna(subset=param_columns + [target_metric]).copy()

    if sample_size is not None and len(plot_df) > sample_size:
        plot_df = plot_df.sample(sample_size, random_state=42)

    dimensions = param_columns + [target_metric]

    fig = px.parallel_coordinates(  # type: ignore[unknownMemberType]
        plot_df,
        dimensions=dimensions,
        color=target_metric,
        color_continuous_scale=px.colors.diverging.RdYlGn,
        title=f"Parameter Combinations — Parallel Coordinates (colored by {target_metric})",
    )
    fig.update_layout()  # type: ignore[unknownMemberType]
    return fig