Skip to content

Commit

Permalink
Add statlines to histogram in Volumetric Analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt committed Jan 23, 2024
1 parent 304cc32 commit 8d22c74
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from ..utils.table_and_figure_utils import (
FLUID_COLORS,
add_histogram_lines,
create_data_table,
create_table_columns,
fluid_annotation,
Expand Down Expand Up @@ -144,6 +145,10 @@ def _update_page_custom(selections: dict, page_selected: str) -> tuple:
)
)

# add lines to the histogram if no subplots
if selections["Plot type"] == "histogram" and selections["Subplots"] is None:
add_histogram_lines(figure, selections["statlines"])

return custom_plotting_layout(
figure=figure,
tables=make_tables(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
from typing import List, Optional, Union

import numpy as np
import plotly.graph_objects as go
import webviz_core_components as wcc
from dash import dash_table
Expand Down Expand Up @@ -173,3 +174,38 @@ def get_text_format_bar_plot(
return f".{selections['decimals']}f"

return f".{selections['decimals']}{selections['textformat']}"


def add_histogram_lines(figure: go.Figure, statline_option: Optional[str]) -> None:
"""Update a histogram figure with vertical lines representing mean/p10/p90"""

def add_line(
figure: go.Figure, x: float, text: str, color: str, dash: bool = False
) -> None:
figure.add_vline(
x=x,
label={
"textposition": "end",
"textangle": 35,
"font": {"size": 14, "color": color},
"yanchor": "bottom",
"xanchor": "right",
"texttemplate": f"<b>{text}</b>",
},
line_width=3,
line_dash="dash" if dash else None,
line_color=color,
)

if statline_option is not None:
for trace in figure.data:
color = trace.marker.line.color
add_line(figure, x=trace.x.mean(), text="Mean", color=color)
if statline_option == "all":
p10 = np.nanpercentile(trace.x, 90)
p90 = np.nanpercentile(trace.x, 10)
add_line(figure, x=p10, text="P10", color=color, dash=True)
add_line(figure, x=p90, text="P90", color=color, dash=True)

# update margin to make room for the labels
figure.update_layout({"margin_t": 100})
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ def histogram_options(uuid: str, tab: str) -> html.Div:
labelStyle={"display": "inline-flex", "margin-right": "5px"},
value="overlay",
),
wcc.RadioItems(
label="Histogram p90/mean/p10 lines:",
id={"id": uuid, "tab": tab, "selector": "statlines"},
options=[
{"label": "none", "value": None},
{"label": "mean", "value": "mean"},
{"label": "all", "value": "all"},
],
labelStyle={"display": "inline-flex", "margin-right": "5px"},
value="all",
),
wcc.Slider(
label="Histogram bins:",
id={"id": uuid, "tab": tab, "selector": "hist_bins"},
Expand Down

0 comments on commit 8d22c74

Please sign in to comment.