Zoom holomap view to current overlay - bokeh

What setting(s) control the default view of individual Bokeh/HoloViews holomap views? For example, create several ellipses, each on their own overlay, combine into one holomap, how to set the default view so that it zooms to the current overlay?
import holoviews as hv
hv.extension('bokeh')
overlays = []
for i in range(10):
overlays.append(hv.Ellipse(i, i, 1))
hm = hv.HoloMap(enumerate(overlays))
^ as individual views are cycled through, I would like to zoom to each circle, rather than the single default view fitting all overlays.
edit: I see that I can control individual default views using .options(xlim=(,), ylim(,)). This works when I view individual holomap views, like hm[0], hm[1], etc., but the view ranges do not change using the slider bar. Is there any way to get the slider bar to honor the xlim/ylim options?

Just add .opts(framewise=True) to normalize per frame of the HoloMap independently, instead of the default of normalizing across all frames of the HoloMap together:
import holoviews as hv
hv.extension('bokeh')
hm = hv.HoloMap(enumerate([hv.Ellipse(i, i, 1).opts(framewise=True) for i in range(10)]))

Related

Select data from a Datashader plot

I'm using Datashader to make crossplots of different parameters due to the quantity of data I'm working with. My idea is to enable some kind of interaction that allows the user to select directly from the plot, the outliers based on the data's tendency showed these ones. I wonder if Holoviews Selection1D stream is compatible with datashader.
This code simulates what I meant:
import numpy as np
import holoviews as hv
from holoviews import opts
from holoviews import streams
from holoviews.operation.datashader import datashade
hv.extension('bokeh')
# Tools to select data
opts.defaults(opts.Points(tools=['box_select', 'lasso_select']))
# Random points to plot
random_points = hv.Points(np.random.randn(1000))
# Holoviews
selection = streams.Selection1D(source=random_points)
# Selected points by Holoviews selection stream
selected_box = hv.DynamicMap(lambda index: random_points.iloc[index],
kdims=[], streams=[selection])
# Final Overlay
Overlay = (random_points * selected_box).opts(padding = 0.01)
Overlay
So far so good, whenever I call selection I get a matrix compounded by the index of the selected points. After Calling the Datashader plot with datashade(Overlay) , the interaction between random_points and selected_box breaks, therefore I'm not sure if this stream is compatible with datashader or if the way I use the stream is wrong!
This is what inspired my idea:
http://holoviews.org/reference/apps/bokeh/selection_stream.html#bokeh-gallery-selection-stream
Helpful example to see the possibilities. Now I know it's not necessary to extract the data from the plot... I could just do another plot using Dynamic maps and streams, either by bounds or selection1D.

How to display the hover information based on a given x-value?

I would like to display the hover information on a stocks plot based on a given x-value instead of the current mouse position.
EDIT: the x-value is set with a slider.
EDIT2:
I am running a folder app and use slider "on change" events. I want to pass the slider value to the plot. There are several plots and the slider just serves the function of highlighting a position simultaneously in all plots. Here is the relevant code:
slider.on_change('value', update_plots)
def update_plots(attr, old, new):
# some code involving 'slider.value'
# e.g. plot.set_hover_by_x_value(slider.value)
EDIT3:
A minimal example would be a single line plot and a slider that is used to highlight a point on the line:
p = figure(plot_width=400, plot_height=400)
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
slider = Slider(start=0, end=5, value=0, step=1, title='x-value')
slider.on_change('value', update_plots)
def update_plots(attr, old, new):
# magic code to highlight the point "slider.value" in "p"
As of Bokeh 1.1 there is no programmatic means to create hover tooltips. They are currently tied to explicit, actual UI interactions (e.g. a mouse moving and hitting a scatter point).
As an alternative, you could use a Label annotation to display information at a given location instead.
There are two kinds of tooltips in Bokeh:
#: Associated with columns in a ColumnDataSource object
$: Special fields such as coordinates of the mouse
In this case you should use #column_with_x_values in your tooltip list for your hovertool to show the given x-values.
More information on the hovertool and examples can be found on this page.

What are Bokeh's 'screen units'?

In Bokeh, what are 'screen units'?
The web isn't forthright on what these creatures are and I've had no luck gleaning their meaning from the (0.9.3) source code
The Bokeh source uses them in its examples, such as this from bokeh/_glyph_functions.py for 'rect':
from bokeh.plotting import figure, output_file, show
plot = figure(width=300, height=300)
plot.rect(x=[1, 2, 3], y=[1, 2, 3], width=10, height=20, color="#CAB2D6",
width_units="screen", height_units="screen")
show(plot)
but I don't see them defined anywhere. What other options would 'width_units' support?
Bokeh lets users set renderer locations either by screen units (related to the pixel distance from the origin of the plot) or data units (which uses the mapper that is used to calculate the plot ranges based on the input data)
This helps in cases like adding box annotations, where sometimes you want the box to be linked to screen (perhaps always in the middle of the screen) or linked to data values (having a box at x=10)
The docs:
https://github.com/bokeh/bokeh/blob/master/bokeh/enums.py#L46

How can I configure the TapTool so it does not hide other data points when clicked?

At the moment when a data point is clicked, all other data points are shaded. Is there a way to prevent this?
fig = fig.circle(x, y)
Ideally, I would like to increase size of the selected circle instead. Is there an easy why to do so?
Update
Seems we cannot change size... according to here:
Only the visual properties of selection_glyph and nonselection_glyph are considered when renderering. Changing
positions, sizes, etc. will have no effect.
However, we can simulate it using line_width property, it becomes more fun if I combine it with line_dish.
As of Bokeh 0.12.15, you can set nonselection_glyph=None when you call a glyph method, for example:
p.circle(x, y, radius=radii, fill_color="navy",
line_color=None, fill_alpha=0.6,
# this is the new part
nonselection_glyph=None)

Bokeh Charts: Custom HoverTool variables for Bar Charts

If I make a bar chart like this:
bar = Bar(data,labels,tools='Hover')
I can make tooltips like this:
hover = bar.select(dict(type=HoverTool))
hover.tooltips = [
('Grade',' $x'),
('Students',' #Students'),
]
However, when I do variables with '#variable' in the tooltips, I am limited to those specified in data. In normal Bokeh plots, I can have custom variables associated with data points by using a ColumnDataSource. Bar does not support ColumnDataSources. Is there another way to make custom variables available in Bokeh chart tooltips (hover)?
There's is an open PR to add this feature to the charts interface.
It's going to be part of the next bokeh release and also be available through the dev builds (or in the master branch of course) as soon as it gets merged if you need it sooner.
I had the same problem. The trick here is to use GlyphRenderer to make the bar chart aware of the data source. For e.g., you can add a line
bar.select(dict(type=GlyphRenderer)) before hover = bar.select(dict(type=HoverTool)). You can then refer your custom variables using #cat, #zero etc. See the following link for more information and a working example.
https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/-aDPTqDPA3M

Resources