|
3 | 3 | import re |
4 | 4 | import time |
5 | 5 |
|
| 6 | +from plotly.subplots import make_subplots |
| 7 | + |
6 | 8 |
|
7 | 9 | def empty_figure(annotation: str = None): |
8 | 10 | fig = go.Figure() |
@@ -39,18 +41,52 @@ def empty_figure(annotation: str = None): |
39 | 41 | ) |
40 | 42 | return fig |
41 | 43 |
|
42 | | -def distance_histo_from_matrix(distances, i, j, color:str ="#00a082"): |
43 | | - fig = go.Figure() |
| 44 | +def distance_histo_from_matrix(distances, i, j, color:str ="#00a082", **kwargs): |
| 45 | + total_counts = distances[0, 0, 0] |
| 46 | + |
| 47 | + hist = distances[i, j] |
| 48 | + q1 = histogram_quantile(hist, 0.25, total_counts) |
| 49 | + median = histogram_quantile(hist, 0.5, total_counts) |
| 50 | + q3 = histogram_quantile(hist, 0.75, total_counts) |
| 51 | + iqr = q3 - q1 |
| 52 | + d_min = hist.nonzero()[0].min() |
| 53 | + d_max = hist.nonzero()[0].max() |
| 54 | + lower_whisker = max(d_min, q1 - 1.5 * iqr) |
| 55 | + upper_whisker = min(d_max, q3 + 1.5 * iqr) |
| 56 | + x = np.arange(hist.shape[-1]) |
| 57 | + weighted_sum = np.sum(hist * x) |
| 58 | + ed = weighted_sum / total_counts |
| 59 | + |
| 60 | + fig = make_subplots(rows=2, shared_xaxes=True, **kwargs) |
44 | 61 | fig.add_trace( |
45 | 62 | go.Bar( |
46 | | - x=np.arange(distances.shape[-1]), |
47 | | - y=distances[i, j] / distances[i, j].sum(), |
| 63 | + x=x, |
| 64 | + y=hist / hist.sum(), |
48 | 65 | marker=dict(color=color), |
49 | | - ) |
| 66 | + name="Histogram", |
| 67 | + ), |
| 68 | + row=1, col=1 |
| 69 | + ) |
| 70 | + fig.add_trace( |
| 71 | + go.Box( |
| 72 | + lowerfence=[float(lower_whisker)], |
| 73 | + q1=[float(q1)], |
| 74 | + median=[float(median)], |
| 75 | + q3=[float(q3)], |
| 76 | + upperfence=[float(upper_whisker)], |
| 77 | + mean=[float(ed)], |
| 78 | + name=f'Box', |
| 79 | + marker=dict(color=color), |
| 80 | + y=["Distribution"], |
| 81 | + boxpoints=False # hide individual points |
| 82 | + ), |
| 83 | + row=2, col=1 |
50 | 84 | ) |
51 | 85 | fig.update_layout( |
52 | | - xaxis=dict(title="Distance [nt]"), |
53 | | - yaxis=dict(title="Probability") |
| 86 | + xaxis2=dict(title="Distance [nt]", showgrid=True), |
| 87 | + xaxis=dict(showgrid=True), |
| 88 | + yaxis=dict(title="Probability", showgrid=True), |
| 89 | + yaxis2=dict(showticklabels=False), |
54 | 90 | ) |
55 | 91 | return fig |
56 | 92 |
|
|
0 commit comments