使用附加元素更新 pandas-bokeh 图表
updating pandas-bokeh charts with additional elements
我正在使用 pandas 和 pandas-bokeh 的 Jupyter 笔记本工作。
我可以毫无问题地创建图表,但是当我想添加一个元素(如 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. :-(
找到答案in the docs(想象一下)。
必须将选项 show_figure=False
添加到图形创建行以防止它在图形创建时显示:
fig = df.plot_bokeh(kind="line", show_figure=False)
后来我调用show(fig)
时它只出现一次
我可以发誓我在发布问题之前曾尝试过。嗯嗯...
我正在使用 pandas 和 pandas-bokeh 的 Jupyter 笔记本工作。
我可以毫无问题地创建图表,但是当我想添加一个元素(如 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. :-(
找到答案in the docs(想象一下)。
必须将选项 show_figure=False
添加到图形创建行以防止它在图形创建时显示:
fig = df.plot_bokeh(kind="line", show_figure=False)
后来我调用show(fig)
我可以发誓我在发布问题之前曾尝试过。嗯嗯...