Alexander Caskie
Alexander Caskie

Reputation: 357

bokeh no graph rendered

I am trying to use the hoovertool using the bokeh package. I have the following code:

from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
))

hover = HoverTool()

hover.tooltips = [
    ("index", "@index"),
    ("(x,y)", "(@x, @y)"),
    ("desc", "@desc"),
]

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='latitude', y_axis_label='longitude')

# Add circle glyphs to figure p
p.circle(x = 'x', y = 'x', size=10, fill_color="grey", line_color=None, 
         hover_fill_color="condition", hover_line_color="white", source = source)

# Create a HoverTool: hover
hover = HoverTool(tooltips=None, mode='vline')

# Add the hover tool to the figure p
p.add_tools(hover)

# Specify the name of the output file and show the result
output_file('hover_glyph.html')
show(p)

When the code runs it opens up a new tab but no graph is present. I have tried putting.

x = [1, 2, 3, 4, 5]; y = [2, 5, 8, 2, 7]
p.circle(x = 'x', y = 'x', size=10, fill_color="grey", line_color=None, 
         hover_fill_color="condition", hover_line_color="white", source = source)

I have looked at these previous questions as well. Jupyter Bokeh: Non-existent column name in glyph renderer but still cannot get it to work. Also, when I run the code from this question a graph is rendered no problem.

Any help would be appreciated, cheers.

Sandy

Upvotes: 0

Views: 275

Answers (1)

Jasper
Jasper

Reputation: 1795

The problem is that you're referring to a column in your ColumnDataSource (condition) does not exist. Your code works by simply defining the condition list. Another problem in your code is that you were defining the hovertool twice, so I also fixed that by removing the first one.

#!/usr/bin/python3
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
    condition=['red', 'blue', 'pink', 'purple', 'grey']
))

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='latitude', y_axis_label='longitude')

# Add circle glyphs to figure p
p.circle(x = 'x', y = 'y', size=10, fill_color="grey", line_color=None, hover_fill_color="condition", hover_line_color="white", source = source)

hover = HoverTool(mode='vline')

hover.tooltips = [
    ("(x,y)", "(@x, @y)"),
    ("desc", "@desc")
]

# Add the hover tool to the figure p
p.add_tools(hover)

# Specify the name of the output file and show the result
output_file('hover_glyph.html')
show(p)

Upvotes: 1

Related Questions