hvPlot.hexbin#
- hvPlot.hexbin(x=None, y=None, C=None, colorbar=True, gridsize=50, logz=False, min_count=None, **kwds)[source]#
The hexbin plot uses hexagons to split the area into several parts and attribute a color to it.
hexbin offers a straightforward method for plotting dense data.
Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.hexbin.html
Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html
- Parameters:
- xstring, optional
Field name to draw x coordinates from. If not specified, the index is used.
- ystring
Field name to draw y-positions from
- Cstring, optional
Field to draw hexbin color from. If not specified a simple count will be used.
- colorbar: boolean, optional
Whether to display a colorbar. Default is True.
- reduce_functionfunction, optional
Function to compute statistics for hexbins, for example
np.mean
. Default aggregation is a count of the values in the area.- gridsize: int or tuple, optional
Number of hexagonal bins along x- and y-axes. Defaults to uniform sampling along both axes when setting and integer but independent bin sampling can be specified a tuple of integers corresponding to the number of bins along each axis. Default is 50.
- logzbool
Whether to apply log scaling to the z-axis. Default is False.
- min_countnumber, optional
The display threshold before a bin is shown, by default bins with a count of less than 1 are hidden
- **kwdsoptional
Additional keywords arguments are documented in Plotting Options. Run
hvplot.help('hexbin')
for the full method documentation.
- Returns:
holoviews.element.HexTiles
/ Panel objectYou can print the object to study its composition and run:
import holoviews as hv hv.help(the_holoviews_object)
to learn more about its parameters and options.
References
Backend-specific styling options#
alpha, cmap, color, fill_alpha, fill_color, hover_alpha, hover_color, hover_fill_alpha, hover_fill_color, hover_line_alpha, hover_line_cap, hover_line_color, hover_line_dash, hover_line_dash_offset, hover_line_join, hover_line_width, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, muted, muted_alpha, muted_color, muted_fill_alpha, muted_fill_color, muted_line_alpha, muted_line_cap, muted_line_color, muted_line_dash, muted_line_dash_offset, muted_line_join, muted_line_width, nonselection_alpha, nonselection_color, nonselection_fill_alpha, nonselection_fill_color, nonselection_line_alpha, nonselection_line_cap, nonselection_line_color, nonselection_line_dash, nonselection_line_dash_offset, nonselection_line_join, nonselection_line_width, scale, selection_alpha, selection_color, selection_fill_alpha, selection_fill_color, selection_line_alpha, selection_line_cap, selection_line_color, selection_line_dash, selection_line_dash_offset, selection_line_join, selection_line_width, visible
alpha, cmap, ec, edgecolor, edgecolors, linewidths, marginals
Examples#
Basic hexbin plot#
import hvplot.pandas # noqa
import pandas as pd
import numpy as np
df = pd.DataFrame({
"x": 2 + 2 * np.random.standard_normal(10000),
"y": 2 + 2 * np.random.standard_normal(10000),
})
df.hvplot.hexbin(aspect=1)
Basic 2D density with earthquake locations#
This example shows how to use hexbin to visualize the spatial density of earthquake events based on latitude and longitude.
import hvplot.pandas # noqa
df = hvplot.sampledata.earthquakes("pandas")
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma",
data_aspect=1, xlim=(120, 130), ylim=(0, 10)
)
Aggregate with a specific variable and function#
In this example the hexbin is colored by the aggregated mean depth, set with the keywords C
and reduce_function
.
import hvplot.pandas # noqa
import numpy as np
df = hvplot.sampledata.earthquakes("pandas")
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma",
data_aspect=1, xlim=(120, 130), ylim=(0, 10),
C='depth', reduce_function=np.mean, clabel='Mean depth',
)
Display threshold and grid size#
Increasing min_count
(default is 1) reduces the number of hexagonal cells plotted.
import hvplot.pandas # noqa
import numpy as np
df = hvplot.sampledata.earthquakes("pandas")
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma", data_aspect=1,
frame_width=300, xlim=(120, 130), ylim=(0, 10),
min_count=1, title='min_count=1',
) +\
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma", data_aspect=1,
frame_width=300, xlim=(120, 130), ylim=(0, 10),
min_count=3, title='min_count=3',
)
Increasing gridsize
(default is 50) increases the number of hexagonal cells plotted. The number of cells in the x and y directions can be customized by setting gridsize
with a tuple.
import hvplot.pandas # noqa
import numpy as np
df = hvplot.sampledata.earthquakes("pandas")
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma", data_aspect=1, frame_width=300,
gridsize=10, title='gridsize=10',
) +\
df.hvplot.hexbin(
x="lon", y="lat", cmap="plasma", data_aspect=1, frame_width=300,
gridsize=(20, 50), title='gridsize=(20, 50)',
)
Xarray example#
This example aggregates temperature readings across longitudes and latitudes using hexagonal bins. logz=True
applies a logarithmic color scale, and gridsize
controls hex resolution.
import hvplot.xarray # noqa
import numpy as np
da = hvplot.sampledata.air_temperature("xarray").sel(time="2014-02-25 12:00")
da.hvplot.hexbin(
x="lon", y="lat", cmap="inferno",
bgcolor="#020210", data_aspect=1, frame_width=400,
C="air", reduce_function=np.mean, clabel="Air temperature (K)",
logz=True, gridsize=30,
)