HexTiles#
Download this notebook from GitHub (right-click to download).
Title: HexTiles Element#
Dependencies Bokeh
Backends Bokeh, Matplotlib
import numpy as np
import holoviews as hv
from holoviews import opts
from holoviews import dim
hv.extension('bokeh')
HexTiles
provide a representation of the data as a bivariate histogram useful for visualizing the structure in larger datasets. The HexTiles
are computed by tessellating the x-, y-ranges of the data, storing the number of points falling in each hexagonal bin. By default HexTiles
simply counts the data in each bin but it also supports weighted aggregations. Currently only linearly spaced bins are supported when using the bokeh backend.
As a simple example we will generate 100,000 normally distributed points and plot them using HexTiles
:
np.random.seed(44)
hex_tiles = hv.HexTiles(np.random.randn(100000, 2))
hex_tiles.opts(opts.HexTiles(width=500, height=400, tools=['hover'], colorbar=True))
Note that the hover shows a Count
by default, representing the number of points falling within each bin.
It is also possible to provide additional value dimensions and define an aggregator
to aggregate by value. If multiple value dimensions are defined the dimension to be colormapped may be defined using the color
option. Note however that multiple value dimensions are allowed and will be displayed in the hover tooltip.
xs, ys = np.random.randn(2, 1000)
hex_with_values = hv.HexTiles((xs, ys, xs*(ys/2.), (xs**2)*ys), vdims=['Values', 'Hover Values'])
overlay = hex_with_values * hv.Points(hex_with_values)
overlay.opts(
opts.HexTiles(width=400, height=400, aggregator=np.sum, tools=['hover']),
opts.Points(size=1, color='black'))
The bokeh backend also supports scaling the size of each hexagon by a value, which can be enabled using the scale
option. Since we do not want zero sized bins we will scale the normalized counts by 0.5 and then add a minimum size of 0.3.
Here we will generate two separate distributions and visualize them by scaling each bin by the ‘Count’.
x, y = np.hstack([np.random.randn(2, 10000), np.random.randn(2, 10000)*0.8+2])
hex_two_distributions = hv.HexTiles((x, y))
hex_two_distributions.opts(scale=(dim('Count').norm()*0.5)+0.3, width=500, height=500)
By default HexTiles
do not plot bins that do not contain any data but using the min_count
option it is possible to plot bins with zero values as well or alternatively set a higher cutoff. Using this options can result in a more visually appealing plot particularly when overlaid with other elements.
Here we will plot the same distributions as above and overlay a Bivariate
plot of the same data:
overlay = hex_two_distributions * hv.Bivariate(hex_two_distributions)
overlay.opts(
opts.HexTiles(min_count=0, width=500, height=500, tools=['hover']),
opts.Bivariate(show_legend=False, line_width=3))
Download this notebook from GitHub (right-click to download).