1
我有一个包含这样一个布尔列大熊猫数据帧:散景情节条件背景颜色
| A | B | C |
| 1 | 3 | True |
| 2 | 4 | True |
| 3 | 4 | False |
| 4 | 1 | False |
| 5 | 2 | True |
我想与基于条件的背景颜色上Y和A上绘制X B的值C 我的意思是这样的:
我可以使用框注释来做到吗?
我有一个包含这样一个布尔列大熊猫数据帧:散景情节条件背景颜色
| A | B | C |
| 1 | 3 | True |
| 2 | 4 | True |
| 3 | 4 | False |
| 4 | 1 | False |
| 5 | 2 | True |
我想与基于条件的背景颜色上Y和A上绘制X B的值C 我的意思是这样的:
我可以使用框注释来做到吗?
是的,有可能使用的BoxAnnotation
的left
和right
参数:
import pandas as pd
from bokeh.plotting import figure, show, output_file, output_notebook
from 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)
>>> A 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
为了简化起见,我在这里添加其他行报复True
计数绘图。
现在,得到含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
最后绘制它:
p = figure(title="Annotations")
p.line(df["A"], df["B"])
for 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)
的解决方案不处理单True
(非连续True
)值,但。但是,您尚未为此方案指定适当的行为。
由于它的工作原理,不得不调整了一下,因为我真正的数据集有日期时间指数,但它现在是好的,非常感谢 – bAN