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
Related
When plotting several hv.Curves using the bokeh backend, when clicking on a legend entry, the corresponding curve gets muted, i.e., its alpha is set to 0.5, so it becomes semi-transpared and, thus, it loses highlight when compared to the unmuted ones.
I've followed the tutorial in the "Working with time series" section of holoviews' user guide on Working with large data and applied holoviews.operation.datashader.rasterize on a hv.Curve, but I'm unable to get the same legend behavior as described above for regular hv.Curves. In fact, I can't even get holoviews to show an entry for the rasterized curve in the legend.
I think I've read in some github issue (can't find the ref now...) that this is somehow related to the fact that rasterize returns some sort of Image object (maybe gv.Image?).
So, the question would be, how does one go about showing a legend entry for a rasterized curve?
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 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)]))
I'm trying to accomplish a heatmap color bar to add extra info about the kdims in my heatmap. (Like the colSideColors option if you are familiar with R's heatmap.2 package.)
I can get a nice result with bokeh backend, but don't know how to get custom (different) element sizes when using matplotlib backend.
Can anyone tell me how to make the strip plot "shorter" (less "high") in the matplotlib backend example?
Setup
import pandas as pd
import numpy as np
import holoviews as hv
hv.extension('bokeh', 'matplotlib')
# dummy data
samples = ['sample{}'.format(x) for x in range(5)]
df = pd.DataFrame(np.random.rand(5, 5),columns=samples, index=samples).reset_index()
df = df.melt(id_vars='index', var_name='y').rename(columns={'index': 'x'})
# column means
df_strip = df.groupby('x').mean().reset_index()
df_strip['y'] = 'dummy'
# make plots
heatmap = hv.HeatMap(df, kdims=['x','y'])
strip = hv.HeatMap(df_strip, kdims=['x','y'])
Result with bokeh
%%output size=100 backend='bokeh'
(strip.options(xaxis=None, yaxis=None, height=50) +
heatmap.options(xrotation=90)).cols(1)
Result with matplotlib backend
%%output size=100 backend='matplotlib'
%%opts Layout [sublabel_format='' vspace=0.1]
(strip.options(xaxis=None, yaxis=None, aspect=1) +
heatmap.options(xrotation=90, aspect=1)).cols(1)
hv.__version__
'1.10.8'
The sizing unfortunately works very differently in the two backends, which means it can be somewhat difficult to get the same behavior. In this particular case you will want to set a larger aspect on the strip plot while also telling the Layout that it should weight the aspect when computing the size of the plots. Doing that looks something like this:
%%output size=100 backend='matplotlib'
%%opts Layout [sublabel_format='' vspace=0.1 aspect_weight=1]
(strip.options(xaxis=None, yaxis=None, aspect=5) +
heatmap.options(xrotation=90, aspect=1)).cols(1)
Suppose I prepare a PDF figure in matplotlib and let us say I have specified the original dimensions of the figure to be 10x10 inches. Would it be possible to produce essentially the same figure, but scaled down to 7x7in (so that all the fonts/point sizes, etc, would scale down appropriately)?
I do understand that I can open my 10x10 file in a vector graphics editor and perform the rescaling, but I was interested whether there is some simple switch that would do this directly from matplotlib.
Use set_size_inches, like so:
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
fig.set_size_inches([10,10])
ax.plot([1,3,2],[2,2,2],'ro-')
plt.savefig('10x10.png')
fig.set_size_inches([4,4])
plt.savefig('4x4.png')