我正在使用 pandas 和 pandas-bokeh 的 Jupyter Notebook 工作。
我可以创建图表没问题,但是当我想添加一个元素(如 Span)时,我不知道如何避免在笔记本中显示图表的第二个副本。
有没有办法解决这些问题:
- 延迟绘制图形,直到我准备好显示()它?
- 更新现有的数字?
- 在绘制第二个图之前删除现有图?
此代码演示了该问题:
# spantest.ipynb
import pandas as pd
import numpy as np
import pandas_bokeh
from bokeh.models import Span
from bokeh.plotting import show
pandas_bokeh.output_notebook()
# Example linechart code from pandas-bokeh docs:
# https://github.com/PatrikHlobil/Pandas-Bokeh#lineplot
np.random.seed(42)
df = pd.DataFrame({"Google": np.random.randn(1000)+0.2,
"Apple": np.random.randn(1000)+0.17},
index=pd.date_range('1/1/2000', periods=1000))
df = df.cumsum()
df = df + 50
fig = df.plot_bokeh(kind="line")
# So far, so good. At this point, I have one chart displayed.
# Now let's say I want to include a Span to indicate some threshold:
span = Span(location=150, dimension='width', line_color='green')
fig.add_layout(span)
show(fig)
# Now I can see the update, but it is displayed as a second copy of the chart. :-(