Mapping bokeh RangeSlider output into different labels - python

I have RangeSlider defined as follows,
slider = RangeSlider(start=0, end=5, value=(0,1), step=1, title="Time")
Is there a way to change the output of the slider so that instead of getting the values 0 to 5, I am able to 'map' these values into some other labels. For instance, whenever the range is 0...4, I get instead t0...t4.
I have not been able to find any information in the bokeh documentation to do this. I am just wondering whether it is at all possible.

I would appear that this is possible but the answer is not simple, and requires going deep into bokeh and javascript. I have found a some mention of this possibility in this video (see video # min 25:30) which lead me to this file in the bokeh repo.

Related

How to add Label/text in Bokeh that can be auto rescaled?

I learnt how to add text by using Label in Bokeh in this question.
However, I found that the text doesn't rescale as I zoom in and out.
The ideal behavior is something like Patches, which becomes larger as you zoom in.
How can I configure for this feature?
Related Questions
Selectively show text in Bokeh plot based on zoom level
As of Bokeh 2.3 scalable text is still an open issue:
https://github.com/bokeh/bokeh/issues/9407
There are some potential partial workarounds discussed there, but nothing that concretely works all the time. Depending on your use case, you could potentially use CustomJS callback on the plot ranges to update the text size that you care about in some way.

Folium Multiple map overlays

I'm fairly new to folium so this might be a bit noobish but I'm currently trying to plot several heatmaps of different data-points and add the ability to switch between the heatmaps all on the same plot. So for example I have such:
# The base map
hmap = folium.Map(location=[38.908111, -77.008871], tiles="Stamen Terrain", zoom_start=12)
# And each layer
# Homicide
HeatMap(list(zip(crime_homicide.LATITUDE.values, crime_homicide.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Homicides').add_to(hmap))
# Robbery
HeatMap(list(zip(crime_robbery.LATITUDE.values, crime_robbery.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Robbery').add_to(hmap))
# Assault
HeatMap(list(zip(crime_assault.LATITUDE.values, crime_assault.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Assault').add_to(hmap))
folium.LayerControl(collapsed=False).add_to(hmap)
folium.GeoJson(dc_shape).add_to(hmap)
I tried using folium's FeatureGroup functionality but it looks like thats only specific markers as opposed to whole maps. Is there a way to switch between different maps if they're all heatmaps?
Your code seems fine.
Try this -
hmap.add_child
Or you can try heatmapwithtime as well, specifying different metrics which you can adjust in realtime to see different heatmaps.
But,FeatureGroup() will not seem to work with HeatMapWithTime and adding layers directly to the heatmap results in multiple time sliders on the side when there should be only one (common) time slider for all added layers.
So if you want to have a single control you'll have to put all your data in a single geojson and use that.
Why do you add a feature group? If you want to be able to select which instance of HeatMapWithTime you want to display, you can add both add them to the map, and they should both turn up in layer control.
m = Map()
HeatMapWithTime(data1).add_to(m)
HeatMapWithTime(data2).add_to(m)
FYI, a feature group is meant to group items and display them together. The items themselves don't get added to the map directly. For example:
fg = FeatureGroup().add_to(m)
fg.add_child(Item1)
fg.add_child(Item2)
Also this is the link, might help you :)
https://python-visualization.github.io/folium/plugins.html

Add Label Annotations on Axes

I currently annotate my charts with the last value of each series by adding a Label and supplying my the name of corresponding range it's plotted on:
Label(
...
x=data.index.max(),
y=data.loc[data.index.max(), 'my_col'],
y_range_name='my_range'
...
)
Which gives me:
How do I move the labels so they are positioned on their respective axis?
Example:
Please note that my labels' y-positioning is off, so I need some help with that aspect too. I've tried tweaking the y_offset but this has not yielded any consistently good results.
My data are always numerical time series.
As of Bokeh 1.2 there is no built-in annotation or glyph that will display outside the central plot area. There is an open issue on GitHub that this is similar to that you can follow or comment on. For the time being, something like this would require making a custom extension

How to create a dynamic labelset on Bokeh that will avoid text overlapping?

I would like to create a Labelset on Bokeh that is going to define the best location in order to avoid the text overlapping.
I tried the following code without success:
labels = LabelSet(x='weight', y='height', text='Name', level='glyph',
x_offset=5, y_offset=5, source=source, render_mode='canvas')
Example of what I do not want
As of Bokeh 1.1 there is nothing built in to Bokeh that will automatically dodge labels. I think this would be a great feature but no one has had the opportunity to work on it yet. There is not a good answer for this currently, there is not much to suggest other than manual experimentation and placement of labels. (And even that can be thrown off if the plot allows zooming.)

How does this Bokeh Column Data Source Work?

I am trying to get a better understanding about the column data source in Bokeh (for Python). I found this code, but I can't seem to find the documentation that explains some things I am looking for, For instance:
Where is the callback from the lasso_select tool? I want to see where the expected functionality is described.
How is the functionality of the lasso_select described in code? (What if I want to change it?)
What is happening to the column data source so that the circles outside the lasso-select region change appearance? (I want to know how I can use the column data source for more complex visualization than is shown by this demo. So I'd like to know what dictionary field is being manipulated, and how is it being manipulated. For example, is there a hidden "color" field or something like that, which isn't explicit in this code?)
What code causes the figure to be redrawn when a lasso_select action is made?
I have many more questions related to this and the CDSView, but I'll stop here for now.
from bokeh.io import output_file, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
output_file("brushing.html")
x = list(range(-20, 21))
y0 = [abs(xx) for xx in x]
y1 = [xx**2 for xx in x]
# create a column data source for the plots to share
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
TOOLS = "box_select,lasso_select,help"
# create a new plot and add a renderer
left = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
left.circle('x', 'y0', source=source)
# create another new plot and add a renderer
right = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
right.circle('x', 'y1', source=source)
p = gridplot([[left, right]])
show(p)
This is related to my previous question, where the only answer was very narrow in explaining for that specific question. However, I am really interested in what's going on under the hood to give the results that are seen. It would help my understanding a lot more if I could know some of those details.
1) There is no callback. The tool is responsible for defining a selection geometry, and and then hit-testing baed on that geometry. The hit test results are store in a selection property of the data source. Glyph renderers draw glyphs based on the selection property of their data source. If two glyph renderers (even on different plots) share the same data source, they will both draw the same set of selected/nonselected as a result.
2) If you mean the appearance of the normal vs selected vs non-selected objects, how to configure that is described in the docs here:
https://docs.bokeh.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs
There are also a few properties on the LassoTool object itself, that control, e.g. whether a selection should be made on every mousemove, or only on mouseup, and what the selection overlay looks like. All of these are recorded in the ReferenceGuide. If you are asking how to change the implementation, as with msot everything in Bokeh, the real work is not done in Python, it is done in the JavaScript library BokehJS. The implementation of the LassoTool is here:
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/lib/models/tools/gestures/lasso_select_tool.ts
If you want something fundamentally different you would need to implement your own custom model, including its JavaScript component. There is an entire User's Guide section about building custom extensions:
https://docs.bokeh.org/en/latest/docs/user_guide/extensions.html
3) The Plot is configured with various Renderers, one of which can be a GlyphRenderer. The GlyphRenderer itself does not draw anything, but it configures various sub-glyphs that are used to draw in specific situations:
glyph draws "normal" versions of glyphs (i.e. when there is no selection on the data source)
selected_glyph draws "selected" versions of glyphs (i.e. the ones inside a lasso or box tool when a selection is active)
nonselected_glyph draws the "non-selected" versions of glyphs (i.e. the ones outside a lasso or box tool when a selection is active) By default the non selection glyph is just a copy of the "normal" glyph with the alpha value set very low.
hover_glyph draws the "hovered" versions of glyphs (i.e. when a hover tool has inspected them)
You configure the appearance in the different situation by configuring properties on the glyphs that are used in each situation. There are sensible defaults for them, but they can be updated as described in the first link of 2)
4) BokehJS has an internal signal/slots event system that is used (among other things) to request canvas redraws whenever various properties change.

Categories