Skip to content

copairs.plot

copairs.plot

Functions to plot percent replicating.

plot(corr_score, percent_score, title, left_null_th=None, right_null_th=None, true_dist_title='True replicates', null_dist_title='Null distribution')

Plot two distributions and threshold(s) line.

Source code in src/copairs/plot.py
11
12
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
72
73
74
75
76
77
78
79
def plot(
    corr_score: CorrelationTestResult,
    percent_score: float,
    title: str,
    left_null_th: Optional[float] = None,
    right_null_th: Optional[float] = None,
    true_dist_title="True replicates",
    null_dist_title="Null distribution",
) -> go.Figure:
    """Plot two distributions and threshold(s) line."""
    # fig = go.Figure()
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    fig.add_trace(
        go.Histogram(
            x=corr_score.corr_dist,
            nbinsx=20,
            # histnorm='probability',
            name=true_dist_title,
        ),
        secondary_y=True,
    )
    fig.add_trace(
        go.Histogram(
            x=corr_score.null_dist,
            nbinsx=100,
            # histnorm='probability',
            name=null_dist_title,
        )
    )
    fig.update_layout(barmode="overlay")
    fig.update_yaxes(title_text=true_dist_title, secondary_y=True)
    fig.update_yaxes(title_text=null_dist_title, secondary_y=False)

    for pos, null_th in [("left", left_null_th), ("right", right_null_th)]:
        if null_th:
            fig.add_vline(
                x=null_th,
                line_width=3,
                line_dash="dash",
                line_color="black",
                annotation_text=f" Null th:{null_th:0.2}",
                annotation_position=f"top {pos}",
            )

    fig.update_traces(opacity=0.75, marker_line_width=1)
    fig.update_layout(
        legend=dict(orientation="h", yanchor="bottom", y=1.05, xanchor="right", x=1)
    )
    fig.update_layout(
        font=dict(
            size=22,
        )
    )

    fig.add_annotation(
        text=f"{title}: {percent_score:0.1%}",
        x=0,
        y=1.06,
        showarrow=False,
        bgcolor="#ffffff",
        xref="paper",
        yref="paper",
        yanchor="bottom",
        xanchor="left",
        font=dict(size=20),
    )

    return fig