python - Bokeh plot conditional background color -
i have pandas dataframe containing boolean column this:
| | b | c | | 1 | 3 | true | | 2 | 4 | true | | 3 | 4 | false | | 4 | 1 | false | | 5 | 2 | true |
and plot values of b on y , on x conditional background color based on c mean :
can box annotation?
yes, possible using left
, right
arguments of boxannotation
:
import pandas pd bokeh.plotting import figure, show, output_file, output_notebook bokeh.models import boxannotation output_notebook() # dummy data df = pd.dataframe({"a": [1, 2, 3, 4, 5, 6], "b": [3, 4, 4, 1, 2, 3], "c": [true, true, false, false, true, true]}) print(df) >>> b c 0 1 3 true 1 2 4 true 2 3 4 false 3 4 1 false 4 5 2 true 5 6 3 true
for simplification, add row here true
counts plotting.
now, consecutive rows containing true
:
df["cons"] = (df["c"].diff(1) != 0).astype('int').cumsum() mask = df["cons"] % 2 == 1 cons_indices = df[mask].groupby("cons").apply(lambda x: x.a.values) print(cons_indices) >>> cons 1 [1, 2] 3 [5, 6] dtype: object
and plot it:
p = figure(title="annotations") p.line(df["a"], df["b"]) cons_index in cons_indices: low_box = boxannotation(left=cons_index.min(), right=cons_index.max(), fill_color="blue") p.add_layout(low_box) show(p)
the solution not deal single true
(non consecutive true
) values, yet. however, haven't specified appropriate behavior scenario.
Comments
Post a Comment