1
votes

I'm fairly new to Bokeh so still probably missing something with it, but I keep running into issues when trying to use Span on multiple plots if I'm generating the plots in a loop. Here's what I'm trying:

titleString = 'Test Plot'
plotVals = [1, 2]
upperLimit = Span(location=6, dimension='width', line_color='red', line_dash='dashed', line_width=1)
lowerLimit = Span(location=-6, dimension='width', line_color='red', line_dash='dashed', line_width=1)
xVals = [0,1,2,3,4]
yVals = [2,4,3,4,2]
for t in enumerate(plotVals):
    print(t[1])
    imgTitle = 'Span Test ' + str(t[0])
    p = figure(title=imgTitle, plot_width=800, plot_height=450, y_range=(-8, 8), x_range=(-4,8))
    p.add_layout(upperLimit)
    p.add_layout(lowerLimit)
    p.circle(xVals,yVals, size=5)            
    show(p)
    reset_output()

The first graph comes out as expected, but the second fails with the following message:

ValueError: object to be added already has 'plot' attribute set

I assume I'm doing something stupid. Can someone point me in the right direction?

1

1 Answers

0
votes

Renderers (including annotations such as Span) may not be shared between multiple plots. You will need to create new spans for each plot.

If you are explicitly trying to reuse span configurations defined outside the loop, you might rewrite similar to:

titleString = 'Test Plot'
plotVals = [1, 2]

upper_kw = dict(location=6, dimension='width', line_color='red', line_dash='dashed', line_width=1)
lower_kw = dict(location=-6, dimension='width', line_color='red', line_dash='dashed', line_width=1)

xVals = [0,1,2,3,4]
yVals = [2,4,3,4,2]
for t in enumerate(plotVals):
    imgTitle = 'Span Test ' + str(t[0])
    p = figure(title=imgTitle, plot_width=800, plot_height=450, y_range=(-8, 8), x_range=(-4,8))

    p.add_layout(Span(**upper_kw))
    p.add_layout(Span(**lower_kw))

    p.circle(xVals, yVals, size=5)            
    show(p)
    reset_output()