"vline" over is not working on thin vbars

While applying “hline” mode of Hovertool it works perfectly for below graph, but while applying “vline” mode on same graph, it doesn’t work on graph rather it display hover on axis for inital and final value. You can get required data from given link [[https://github.com/internshipdeco/sample_data]] and code as below

from bokeh.io import show, output_file, curdoc
import dateutil
from bokeh.models import HoverTool
import pandas as pd
import pymongo
import datetime
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
dataframe = pd.read_csv("sample.csv")
#some preprocessing 
if dataframe['VOLUME'].max() >= 100000:
   dataframe["VOLUME"] = dataframe["VOLUME"].__div__(100)
else:
   dataframe["VOLUME"] = dataframe["VOLUME"]
dataframe['DATE'] = pd.to_datetime(dataframe['DATE'])
p = ColumnDataSource(data=dict(DATE=[], VOLUME=[]))
p.data = p.from_df(dataframe)

ts2 = figure(sizing_mode='stretch_both', x_axis_type='datetime', active_drag="auto", 
           x_axis_label='DATE', y_axis_label='VOLUME')
t1 = ts2.vbar(x ='DATE', top= 'VOLUME',width = 1, bottom = 0,  legend='volume', 
               color='red', source=p)
ts2.grid.grid_line_alpha = 0.8
ts2.add_tools(HoverTool(renderers=[t1],
                    tooltips=[("Date", "@DATE{%F}"), ("Volume", "@VOLUME{0.2f}")],
                    formatters={"DATE": "datetime"},
                    mode='vline'))
#ts2.add_tools(hover_tool)

ts2.x_range.start = dataframe.DATE.min().timestamp() * 1000
ts2.x_range.end = dataframe.DATE.max().timestamp() * 1000
curdoc().add_root(ts2)

Hope all this information will be good enough now.

@internshipdeco I tried to run your code. First I had to remove an unused pymongo import, then after that I got a KeyError despite having saved your sample data file, because the raw file on GitHub contained a formatting problem. After fixing that, there is still this error: AttributeError: ‘str’ object has no attribute ‘strftime’ Please always verify that example code is runnable. Runnable code is the number one thing that helps others help you.

Fortunately in this particular case, I can probably guess what the issue is just by glancing at the code. The units of a datetime axis are milliseconds since epoch. You have set your bars to be width=1. That is one millisecond wide, on a plot scale that covers months. The only reason they show up at all, is that you have specified a line color (the color parameter sets both fill and line color) and setting any line color at all forces the HTML canvas to draw an outline which ends up being at least one pixel wide. However, the spatial indexing for hit-testing for the hover tool happens in data space, where the width of your bars makes them essentially invisible, because there are far, far more milliseconds in a month than there are pixels wide on your plot. You will need to make your bars much, much, much wider for them to be “visible” to an hline hover tool.

For instance, if you wanted to make the bars “one day” wide, you would need to set:

#       ms/sec * sec/min * min/hr * hr/day
width = 1000   * 60      * 60     * 24      # = 86400000 

Thank you so much @Bryan, your guess was absolutely correct. After changing width size, hover works perfectly. Sorry for issue in the example code. I have updated the same hope that works for you.

Glad it’s working now!