hvPlot.heatmap#
- hvPlot.heatmap(x=None, y=None, C=None, colorbar=True, logz=False, **kwds)[source]#
heatmap visualises tabular data indexed by two key dimensions as a grid of colored values. This allows spotting correlations in multivariate data and provides a high-level overview of how the two variables are plotted. The data can either be shaped as a 2-D array (in which case no aggregate will be computed) or as a set of two axis variables and one aggregation variable (on which an aggregation is computed).
Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.heatmap.html
Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html
- Parameters:
- xstring, optional
Field name to draw x-positions from. In the data as an array case, setting x to None assumes drawing x labels from the column names, which can be explicitly declared by setting x to
'columns'
. Can refer to continuous and categorical data.- ystring, optional
Field name to draw y-positions from. In the data as an array case, setting y to None assumes drawing y labels from the index, which can be explicitly declared by setting y to
'index'
or to the index name. Can refer to continuous and categorical data.- Cstring, optional
Field to draw heatmap color from. If not specified a simple count will be used.
- colorbar: boolean, optional
Whether to display a colorbar. Default is True.
- logzbool
Whether to apply log scaling to the z-axis. Default is False.
- reduce_functionfunction, optional
Function to compute statistics for heatmap, for example
np.mean
. If omitted, no aggregation is applied and duplicate values are dropped.- **kwdsoptional
Additional keywords arguments are documented in Plotting Options. Run
hvplot.help('heatmap')
for the full method documentation.
- Returns:
holoviews.element.HeatMap
/ 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, annular_alpha, annular_color, annular_fill_alpha, annular_fill_color, annular_hover_alpha, annular_hover_color, annular_hover_fill_alpha, annular_hover_fill_color, annular_hover_line_alpha, annular_hover_line_cap, annular_hover_line_color, annular_hover_line_dash, annular_hover_line_dash_offset, annular_hover_line_join, annular_hover_line_width, annular_line_alpha, annular_line_cap, annular_line_color, annular_line_dash, annular_line_dash_offset, annular_line_join, annular_line_width, annular_muted, annular_muted_alpha, annular_muted_color, annular_muted_fill_alpha, annular_muted_fill_color, annular_muted_line_alpha, annular_muted_line_cap, annular_muted_line_color, annular_muted_line_dash, annular_muted_line_dash_offset, annular_muted_line_join, annular_muted_line_width, annular_nonselection_alpha, annular_nonselection_color, annular_nonselection_fill_alpha, annular_nonselection_fill_color, annular_nonselection_line_alpha, annular_nonselection_line_cap, annular_nonselection_line_color, annular_nonselection_line_dash, annular_nonselection_line_dash_offset, annular_nonselection_line_join, annular_nonselection_line_width, annular_selection_alpha, annular_selection_color, annular_selection_fill_alpha, annular_selection_fill_color, annular_selection_line_alpha, annular_selection_line_cap, annular_selection_line_color, annular_selection_line_dash, annular_selection_line_dash_offset, annular_selection_line_join, annular_selection_line_width, annular_visible, cmap, color, dilate, 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, 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, ticks_text_align, ticks_text_alpha, ticks_text_baseline, ticks_text_color, ticks_text_font, ticks_text_font_size, ticks_text_font_style, visible, xmarks_alpha, xmarks_color, xmarks_hover_alpha, xmarks_hover_color, xmarks_hover_line_alpha, xmarks_hover_line_cap, xmarks_hover_line_color, xmarks_hover_line_dash, xmarks_hover_line_dash_offset, xmarks_hover_line_join, xmarks_hover_line_width, xmarks_line_alpha, xmarks_line_cap, xmarks_line_color, xmarks_line_dash, xmarks_line_dash_offset, xmarks_line_join, xmarks_line_width, xmarks_muted, xmarks_muted_alpha, xmarks_muted_color, xmarks_muted_line_alpha, xmarks_muted_line_cap, xmarks_muted_line_color, xmarks_muted_line_dash, xmarks_muted_line_dash_offset, xmarks_muted_line_join, xmarks_muted_line_width, xmarks_nonselection_alpha, xmarks_nonselection_color, xmarks_nonselection_line_alpha, xmarks_nonselection_line_cap, xmarks_nonselection_line_color, xmarks_nonselection_line_dash, xmarks_nonselection_line_dash_offset, xmarks_nonselection_line_join, xmarks_nonselection_line_width, xmarks_selection_alpha, xmarks_selection_color, xmarks_selection_line_alpha, xmarks_selection_line_cap, xmarks_selection_line_color, xmarks_selection_line_dash, xmarks_selection_line_dash_offset, xmarks_selection_line_join, xmarks_selection_line_width, xmarks_visible, ymarks_alpha, ymarks_color, ymarks_hover_alpha, ymarks_hover_color, ymarks_hover_line_alpha, ymarks_hover_line_cap, ymarks_hover_line_color, ymarks_hover_line_dash, ymarks_hover_line_dash_offset, ymarks_hover_line_join, ymarks_hover_line_width, ymarks_line_alpha, ymarks_line_cap, ymarks_line_color, ymarks_line_dash, ymarks_line_dash_offset, ymarks_line_join, ymarks_line_width, ymarks_muted, ymarks_muted_alpha, ymarks_muted_color, ymarks_muted_line_alpha, ymarks_muted_line_cap, ymarks_muted_line_color, ymarks_muted_line_dash, ymarks_muted_line_dash_offset, ymarks_muted_line_join, ymarks_muted_line_width, ymarks_nonselection_alpha, ymarks_nonselection_color, ymarks_nonselection_line_alpha, ymarks_nonselection_line_cap, ymarks_nonselection_line_color, ymarks_nonselection_line_dash, ymarks_nonselection_line_dash_offset, ymarks_nonselection_line_join, ymarks_nonselection_line_width, ymarks_selection_alpha, ymarks_selection_color, ymarks_selection_line_alpha, ymarks_selection_line_cap, ymarks_selection_line_color, ymarks_selection_line_dash, ymarks_selection_line_dash_offset, ymarks_selection_line_join, ymarks_selection_line_width, ymarks_visible
alpha, annular_edgecolors, annular_linewidth, clims, cmap, ec, edgecolor, edgecolors, hatch, linestyles, linewidths, norm, shading, visible, xmarks_edgecolor, xmarks_linewidth, ymarks_edgecolor, ymarks_linewidth
Examples#
Heatmaps are ideal for visualizing values across two dimensions as a color-coded matrix. They are commonly used for temporal data, correlation grids, and density summaries.
Basic heatmap plot with aggregation#
The heatmap
plot can aggregate the data when provided in long form.
import hvplot.pandas # noqa
import numpy as np
import pandas as pd
df = pd.DataFrame({
"cat1": ["A", "A", "A", "B", "B", "B"],
"cat2": ["X", "X", "Y", "Y", "Y", "Y"],
"values": [1, 2, 3, 4, 5, 6],
})
df.hvplot.heatmap(x="cat1", y="cat2", C="values", reduce_function=np.mean)
Basic heatmap plot without aggregation#
No aggregation is required when the data is provided in wide format. The color/tooltip of a cell is derived from its corresponding value in the dataset.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame(
{"2020": [10, 35], "2021": [40, 30], "2022": [20, 25]},
index=["A", "B"],
)
df.hvplot.heatmap()
Heatmap of earthquake counts by depth and magnitude#
Let’s use a more realistic dataset. This plot visualizes the count of earthquakes categorized by both depth and magnitude class. It gives a clear overview of how often each type of earthquake occurs.
import hvplot.pandas # noqa
import numpy as np
df = hvplot.sampledata.earthquakes('pandas')
df.hvplot.heatmap(
x='mag_class',
y='depth_class',
C='mag',
reduce_function=np.size,
fields={'mag': 'count'}, # for the tooltip to display count instead of mag
)
Use of reduce_function
#
reduce_function
specifies the function used to aggregate the values of C
for each combination of x
and y
after grouping. Numpy functions are appropriate, like in the example below with np.nanmedian
.
import hvplot.pandas # noqa
df = hvplot.sampledata.penguins('pandas')
df.hvplot.heatmap(
x='species', y='island', C='body_mass_g',
reduce_function=np.nanmedian, clabel='Median body mass (g)',
)
Use of logz
#
This version applies a logarithmic color scale to better distinguish lower-frequency combinations, which may be overshadowed by dominant bins in a linear scale.
import hvplot.pandas # noqa
import numpy as np
df = hvplot.sampledata.earthquakes('pandas')
df.hvplot.heatmap(
x='mag_class', y='depth_class', C='mag', logz=True,
reduce_function=np.size, fields={'mag': 'count'}
)
More information when no aggregation#
x
and y
can be set differently in this case.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame(
{"2020": [10, 35], "2021": [40, 30], "2022": [20, 25]},
index=["A", "B"],
)
df.index.name = "Group"
plot_opts = dict(xaxis="top", width=400, height=200, flip_yaxis=True)
(
df.hvplot.heatmap(**plot_opts, title="From array, without setting x and y") +
df.hvplot.heatmap(x="columns", y="index", title="From array, setting columns/index", **plot_opts) +
df.hvplot.heatmap(x="columns", y="Group", title="From array, setting columns/index name", **plot_opts)
).cols(1)