hvPlot.bivariate#

hvPlot.bivariate(x=None, y=None, colorbar=True, bandwidth=None, cut=3, filled=False, levels=10, **kwds)[source]#

A bivariate plot uses nested contours (or contours combined with color) to indicate regions of higher local density.

Bivariate plots provide a convenient way to visualize a 2D distribution of values as a Kernel Density Estimate (KDE) and therefore provide a 2D extension to kde(). KDE is a non-parametric way to estimate the probability density function of a random variable.

The KDE works by placing a Gaussian kernel at each sample with the supplied bandwidth, which are then summed to produce the density estimate. By default the bandwidth is determined using the Scott’s method, which usually produces good results, but it may be overridden by an explicit value.

Bivariate plots can be a useful alternative to scatter plots, if the data are too dense to plot each point individually.

Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.bivariate.html

Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html

Parameters:
xstring, optional

Field name to draw x-positions from. If not specified, the index is used.

ystring, optional

Field name to draw y-positions from

colorbarboolean

Whether to display a colorbar

bandwidthfloat, optional

Allows supplying explicit bandwidth value of the kernel for the density estimate, rather than relying on Scott. Higher value yields smoother contours. Default is None.

cutfloat, optional

Draw the estimate to cut * bw from the extreme data points. Default is 3.

filledbool, optional

If True the contours will be filled. Default is False.

levelsint or list, optional

The number of contour lines to draw or a list of scalar values used to specify the contour levels. Default is 10.

**kwdsoptional

Additional keywords arguments are documented in Plotting Options. Run hvplot.help('bivariate') for the full method documentation.

Returns:
holoviews.element.Bivariate / Panel object

You 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.

Notes

This function requires scipy to be installed.

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, 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, c, capstyle, cmap, color, ec, ecolor, edgecolor, facecolor, fc, fill, hatch, joinstyle, linestyle, linewidth, lw

Examples#

Basic bivariate plot#

import hvplot.pandas # noqa
import numpy as np
import pandas as pd

x, y = np.random.multivariate_normal([0, 0], [[1, 0.8], [0.8, 1]], 1000).T
df = pd.DataFrame({"x": x, "y": y})

df.hvplot.bivariate(data_aspect=1)

Bivariate distribution of penguins bill length and depth#

This example shows the joint distribution of bill length and bill depth in penguins using a 2D density estimate. Note colorbar=True by default.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")

df.hvplot.bivariate(x='bill_length_mm', y='bill_depth_mm', width=400)

The plot below displays the contour lines of a bivariate plot overlaid with a scatter plot. The contour lines help highlight the 3 clusters present in the dataset.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")

(
    df.hvplot.bivariate(x='bill_length_mm', y='bill_depth_mm', width=400, cmap='grey_r')
    * df.hvplot.scatter(x='bill_length_mm', y='bill_depth_mm', alpha=0.1, color='green')
).opts(show_legend=False) 

The filled parameter specifies whether the contours should be filled.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")

df.hvplot.bivariate(x='bill_length_mm', y='bill_depth_mm', filled=True, cmap='viridis', width=400)

Control smoothing with bandwidth#

You can control the smoothness of the estimate using the bandwidth argument that accepts a positive numerical value. Smaller values yield more detail. When not set, the bandwidth is internally computed using Scott’s rule of thumb.

import hvplot.pandas #  noqa

df = hvplot.sampledata.penguins("pandas")

plot_opts = dict(
    x='bill_length_mm', y='bill_depth_mm',
    colorbar=False, frame_width=200, aspect=1,
)
df.hvplot.bivariate(bandwidth=0.2, title='bandwidth=0.2', **plot_opts) +\
df.hvplot.bivariate(bandwidth=0.6, title='bandwidth=0.6', **plot_opts)

Control binning with levels#

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")

plot_opts = dict(
    x='bill_length_mm', y='bill_depth_mm',
    colorbar=False, frame_width=200, aspect=1,
)
df.hvplot.bivariate(levels=5, title='levels=5', **plot_opts) +\
df.hvplot.bivariate(
    levels=[0.001, 0.002, 0.004, 0.006, 0.008, 0.01, 0.1],
    title='levels as list', **plot_opts
)

Control evaluation extent with cut#

cut is a factor, multiplied by the smoothing bandwidth, that determines how far the evaluation grid extends past the extreme datapoints. When set to 0, the contours are truncated at the data limits.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins("pandas")

(
    df.hvplot.bivariate(x='bill_length_mm', y='bill_depth_mm', cut=0, title='cut=0')
    * df.hvplot.scatter(x='bill_length_mm', y='bill_depth_mm', alpha=0.2, color='grey')
).opts(show_legend=False) 
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.