Spaces:
Sleeping
Sleeping
dzs commited on
Commit ·
813741b
1
Parent(s): b2fee3f
added dynamic log ticks
Browse files
figure.py
CHANGED
|
@@ -2,6 +2,41 @@ import plotly.graph_objects as go
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
| 6 |
|
| 7 |
fig = go.Figure()
|
|
@@ -40,7 +75,7 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 40 |
marker=dict(color="blue"),
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Upper extent
|
| 44 |
y1_upper = (
|
| 45 |
dataframe["Condition 1"].to_numpy(dtype=float)
|
| 46 |
+ dataframe["Cond1_err_plus"].to_numpy(dtype=float)
|
|
@@ -58,10 +93,15 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 58 |
min_of_piecewise_max = 1.0
|
| 59 |
|
| 60 |
y_min = float(min_of_piecewise_max * 1e-1)
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
fig.update_layout(
|
| 64 |
barmode="group",
|
|
|
|
| 65 |
legend=dict(
|
| 66 |
orientation="h",
|
| 67 |
x=0.5,
|
|
@@ -69,13 +109,17 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 69 |
y=1.08,
|
| 70 |
font=dict(size=18)
|
| 71 |
),
|
|
|
|
| 72 |
yaxis=dict(
|
| 73 |
title=dict(
|
| 74 |
text="Fraction released",
|
| 75 |
font=dict(size=18)
|
| 76 |
),
|
| 77 |
type="log",
|
| 78 |
-
range=[
|
|
|
|
|
|
|
|
|
|
| 79 |
),
|
| 80 |
)
|
| 81 |
|
|
@@ -86,6 +130,7 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 86 |
x=dataframe["CASRN"],
|
| 87 |
name="Condition 1 / Condition 2",
|
| 88 |
orientation="v",
|
|
|
|
| 89 |
error_y=dict(
|
| 90 |
type="data",
|
| 91 |
symmetric=False,
|
|
@@ -93,17 +138,25 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 93 |
arrayminus=dataframe["Ratio_err_minus"],
|
| 94 |
visible=True,
|
| 95 |
),
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
fig.add_hline(
|
| 101 |
y=1.0,
|
| 102 |
-
line_width=
|
| 103 |
line_dash="dash",
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
-
# Include complete IQR
|
| 107 |
ratio_lower = (
|
| 108 |
dataframe["Ratio"].to_numpy(dtype=float)
|
| 109 |
- dataframe["Ratio_err_minus"].to_numpy(dtype=float)
|
|
@@ -115,44 +168,68 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 115 |
)
|
| 116 |
|
| 117 |
positive_lower = ratio_lower[
|
| 118 |
-
np.isfinite(ratio_lower)
|
|
|
|
| 119 |
]
|
| 120 |
|
| 121 |
positive_upper = ratio_upper[
|
| 122 |
-
np.isfinite(ratio_upper)
|
|
|
|
| 123 |
]
|
| 124 |
|
| 125 |
if len(positive_lower) > 0:
|
| 126 |
-
y_min = np.nanmin(positive_lower) / 2
|
| 127 |
else:
|
| 128 |
y_min = 0.1
|
| 129 |
|
| 130 |
if len(positive_upper) > 0:
|
| 131 |
-
y_max = np.nanmax(positive_upper) * 2
|
| 132 |
else:
|
| 133 |
y_max = 10.0
|
| 134 |
|
| 135 |
-
#
|
| 136 |
y_min = min(y_min, 0.5)
|
| 137 |
y_max = max(y_max, 2.0)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
fig.update_layout(
|
| 140 |
barmode="group",
|
| 141 |
showlegend=False,
|
|
|
|
| 142 |
yaxis=dict(
|
| 143 |
title=dict(
|
| 144 |
text="Fraction released ratio (C1 / C2)",
|
| 145 |
font=dict(size=18)
|
| 146 |
),
|
|
|
|
| 147 |
type="log",
|
|
|
|
| 148 |
range=[
|
| 149 |
np.log10(y_min),
|
| 150 |
np.log10(y_max)
|
| 151 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
),
|
| 153 |
)
|
| 154 |
|
|
|
|
| 155 |
# Common layout
|
|
|
|
|
|
|
| 156 |
fig.update_layout(
|
| 157 |
xaxis=dict(
|
| 158 |
title=dict(
|
|
@@ -161,13 +238,25 @@ def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
|
| 161 |
font=dict(size=18)
|
| 162 |
)
|
| 163 |
),
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
autosize=False,
|
| 166 |
height=600,
|
| 167 |
width=800,
|
| 168 |
)
|
| 169 |
|
| 170 |
-
fig.update_xaxes(
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
return fig
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
def make_log_ticks(y_min, y_max):
|
| 6 |
+
|
| 7 |
+
min_exp = int(np.floor(np.log10(y_min)))
|
| 8 |
+
max_exp = int(np.ceil(np.log10(y_max)))
|
| 9 |
+
|
| 10 |
+
n_decades = max_exp - min_exp
|
| 11 |
+
|
| 12 |
+
# Use more ticks for narrow ranges,
|
| 13 |
+
# fewer ticks when many orders of magnitude are shown
|
| 14 |
+
if n_decades <= 2:
|
| 15 |
+
multipliers = [1, 2, 5]
|
| 16 |
+
else:
|
| 17 |
+
multipliers = [1]
|
| 18 |
+
|
| 19 |
+
ticks = []
|
| 20 |
+
|
| 21 |
+
for exp in range(min_exp, max_exp + 1):
|
| 22 |
+
|
| 23 |
+
decade = 10.0 ** exp
|
| 24 |
+
|
| 25 |
+
for mult in multipliers:
|
| 26 |
+
|
| 27 |
+
value = mult * decade
|
| 28 |
+
|
| 29 |
+
if y_min <= value <= y_max:
|
| 30 |
+
ticks.append(value)
|
| 31 |
+
|
| 32 |
+
# Make sure ratio = 1 is included when visible
|
| 33 |
+
if y_min <= 1.0 <= y_max and 1.0 not in ticks:
|
| 34 |
+
ticks.append(1.0)
|
| 35 |
+
|
| 36 |
+
ticks.sort()
|
| 37 |
+
|
| 38 |
+
return ticks
|
| 39 |
+
|
| 40 |
def build_figure(dataframe: pd.DataFrame, mc_option="absolute") -> go.Figure:
|
| 41 |
|
| 42 |
fig = go.Figure()
|
|
|
|
| 75 |
marker=dict(color="blue"),
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Upper extent including error bars
|
| 79 |
y1_upper = (
|
| 80 |
dataframe["Condition 1"].to_numpy(dtype=float)
|
| 81 |
+ dataframe["Cond1_err_plus"].to_numpy(dtype=float)
|
|
|
|
| 93 |
min_of_piecewise_max = 1.0
|
| 94 |
|
| 95 |
y_min = float(min_of_piecewise_max * 1e-1)
|
| 96 |
+
|
| 97 |
+
y_min = max(
|
| 98 |
+
y_min,
|
| 99 |
+
np.nextafter(0.0, 1.0)
|
| 100 |
+
)
|
| 101 |
|
| 102 |
fig.update_layout(
|
| 103 |
barmode="group",
|
| 104 |
+
|
| 105 |
legend=dict(
|
| 106 |
orientation="h",
|
| 107 |
x=0.5,
|
|
|
|
| 109 |
y=1.08,
|
| 110 |
font=dict(size=18)
|
| 111 |
),
|
| 112 |
+
|
| 113 |
yaxis=dict(
|
| 114 |
title=dict(
|
| 115 |
text="Fraction released",
|
| 116 |
font=dict(size=18)
|
| 117 |
),
|
| 118 |
type="log",
|
| 119 |
+
range=[
|
| 120 |
+
np.log10(y_min),
|
| 121 |
+
np.log10(1.0)
|
| 122 |
+
]
|
| 123 |
),
|
| 124 |
)
|
| 125 |
|
|
|
|
| 130 |
x=dataframe["CASRN"],
|
| 131 |
name="Condition 1 / Condition 2",
|
| 132 |
orientation="v",
|
| 133 |
+
|
| 134 |
error_y=dict(
|
| 135 |
type="data",
|
| 136 |
symmetric=False,
|
|
|
|
| 138 |
arrayminus=dataframe["Ratio_err_minus"],
|
| 139 |
visible=True,
|
| 140 |
),
|
| 141 |
+
|
| 142 |
+
marker=dict(
|
| 143 |
+
color="lightskyblue",
|
| 144 |
+
line=dict(
|
| 145 |
+
color="blue",
|
| 146 |
+
width=1
|
| 147 |
+
)
|
| 148 |
+
),
|
| 149 |
)
|
| 150 |
|
| 151 |
+
# Reference line: ratio = 1
|
| 152 |
fig.add_hline(
|
| 153 |
y=1.0,
|
| 154 |
+
line_width=2,
|
| 155 |
line_dash="dash",
|
| 156 |
+
line_color="black",
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# Include the complete IQR when determining axis limits
|
| 160 |
ratio_lower = (
|
| 161 |
dataframe["Ratio"].to_numpy(dtype=float)
|
| 162 |
- dataframe["Ratio_err_minus"].to_numpy(dtype=float)
|
|
|
|
| 168 |
)
|
| 169 |
|
| 170 |
positive_lower = ratio_lower[
|
| 171 |
+
np.isfinite(ratio_lower)
|
| 172 |
+
& (ratio_lower > 0)
|
| 173 |
]
|
| 174 |
|
| 175 |
positive_upper = ratio_upper[
|
| 176 |
+
np.isfinite(ratio_upper)
|
| 177 |
+
& (ratio_upper > 0)
|
| 178 |
]
|
| 179 |
|
| 180 |
if len(positive_lower) > 0:
|
| 181 |
+
y_min = np.nanmin(positive_lower) / 2.0
|
| 182 |
else:
|
| 183 |
y_min = 0.1
|
| 184 |
|
| 185 |
if len(positive_upper) > 0:
|
| 186 |
+
y_max = np.nanmax(positive_upper) * 2.0
|
| 187 |
else:
|
| 188 |
y_max = 10.0
|
| 189 |
|
| 190 |
+
# Always leave some room around ratio = 1
|
| 191 |
y_min = min(y_min, 0.5)
|
| 192 |
y_max = max(y_max, 2.0)
|
| 193 |
|
| 194 |
+
# Generate appropriate tick locations
|
| 195 |
+
ticks = make_log_ticks(
|
| 196 |
+
y_min,
|
| 197 |
+
y_max
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
fig.update_layout(
|
| 201 |
barmode="group",
|
| 202 |
showlegend=False,
|
| 203 |
+
|
| 204 |
yaxis=dict(
|
| 205 |
title=dict(
|
| 206 |
text="Fraction released ratio (C1 / C2)",
|
| 207 |
font=dict(size=18)
|
| 208 |
),
|
| 209 |
+
|
| 210 |
type="log",
|
| 211 |
+
|
| 212 |
range=[
|
| 213 |
np.log10(y_min),
|
| 214 |
np.log10(y_max)
|
| 215 |
+
],
|
| 216 |
+
|
| 217 |
+
tickmode="array",
|
| 218 |
+
tickvals=ticks,
|
| 219 |
+
ticktext=[
|
| 220 |
+
f"{x:g}"
|
| 221 |
+
for x in ticks
|
| 222 |
+
],
|
| 223 |
+
|
| 224 |
+
gridcolor="lightgray",
|
| 225 |
+
gridwidth=1,
|
| 226 |
),
|
| 227 |
)
|
| 228 |
|
| 229 |
+
# ----------------------------
|
| 230 |
# Common layout
|
| 231 |
+
# ----------------------------
|
| 232 |
+
|
| 233 |
fig.update_layout(
|
| 234 |
xaxis=dict(
|
| 235 |
title=dict(
|
|
|
|
| 238 |
font=dict(size=18)
|
| 239 |
)
|
| 240 |
),
|
| 241 |
+
|
| 242 |
+
margin=dict(
|
| 243 |
+
l=50,
|
| 244 |
+
r=50,
|
| 245 |
+
t=20,
|
| 246 |
+
b=20
|
| 247 |
+
),
|
| 248 |
+
|
| 249 |
autosize=False,
|
| 250 |
height=600,
|
| 251 |
width=800,
|
| 252 |
)
|
| 253 |
|
| 254 |
+
fig.update_xaxes(
|
| 255 |
+
tickfont=dict(size=14)
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
fig.update_yaxes(
|
| 259 |
+
tickfont=dict(size=14)
|
| 260 |
+
)
|
| 261 |
|
| 262 |
return fig
|