I love how easy it is to set up basic hover feedback with HoverTool, but I’m wrestling with a couple aspects of the display. I have time-series data, with measurements that represent amounts in US$. This data starts out life as a pandas.Series
. Legible plotting is easy (following assumes jupyter notebook):
p = figure(title='Example currency', x_axis_type='datetime',
plot_height=200, plot_width=600, tools='')
p.line(my_data.index, my_data)
p.yaxis[0].formatter = NumeralTickFormatter(format='$0,0')
show(p)
This shows me the time-series, with date-formatting on the x-axis and y-axis values that look like “200,000”, “$250,000”, etc. I have two questions about HoverTool
behavior:
- Controlling formatting for
$x
and$y
. - Accessing the name of the dataset under the cursor.
Simply adding aHoverTool
allows me to see values, but in unhelpful units:
p.add_tools(HoverTool())
The corresponding tooltip values with these defaults show “1.468e+5” rather than “$146,800” (or even “146800”, the underlying Series
value); similarly, the date value appears as “1459728000000” rather than (say) “2016-04-04”. I can manually work around this display issue by making my pandas.Series
into a ColumnDataSource
and adding string columns with the desired formatting:
# Make sure Series and its index have `name`, before converting to DataFrame
[my_data.name](http://my_data.name) = 'revenue'
[my_data.index.name](http://my_data.index.name) = 'day'
df = my_data.reset_index()
# Add str columns for tooltip display
df['daystr'] = df['day'].dt.strftime('%m %b %Y')
df['revstr'] = df['revenue'].apply(lambda x: '${:,d}'.format(int(x)))
cds = ColumnDataSource(df)
p = figure(title='Example currency', x_axis_type='datetime',
plot_height=200, plot_width=600, tools='')
p.line('day', 'revenue', source=cds)
p.yaxis[0].formatter = NumeralTickFormatter(format='$0,0')
p.add_tools(HoverTool(tooltips=[('Amount', '@revstr'), ('Day', '@daystr')]))
show(p)
but is there a way to handle the formatting in the HoverTool
configuration instead? That seems much more desirable than all the data-set transformation that’s required above. I looked through the documentation and (quickly) scanned through the source, and didn’t see anything obvious that would save me from building the “output” columns as above.
Related to that, when I have several lines in a single plot, is there any way for me to access the name
(or perhaps legend
value) of each line within HoverTool.tooltips
? It would be extremely helpful to include something in the tooltip to differentiate which dataset values are coming from, rather than needing to rely on (say) line-color in conjunction with the tool-tip display. For now, I’ve added an additional column to the ColumnDataSource
that’s just the string value I want to show; that obviously only works for datasets that include a single measurement column. When multiple lines are sharing an underlying ColumnDataSource
, it would be sufficient to access the column-name that’s provided to y
.
Thank you for any insights or suggestions.
-mt