I'm working in a Jupyter Notebook with pandas and pandas-bokeh.
I can create a chart no problem, but when I want to add an element (like a Span), I can't figure out how to avoid getting a second copy of the chart displayed in the notebook.
Is there a way to any of these:
- Delay drawing the figure until I'm ready to show() it?
- Update the existing figure in place?
- Delete the existing figure before drawing the second figure?
This code demonstrates the issue:
# 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. :-(
Found the answer in the docs (imagine that).
Had to add the option
show_figure=False
to the figure creation line to prevent it from being shown on figure creation:Then later it appears only once when I call
show(fig)
I could swear I tried that at some point before posting the question. Ah well...