Legend Options#

Parameters

Description

legend (bool or str or None, default=None)

Whether to show a legend, or a legend position. A cardinal position ('top', 'bottom', 'left', 'right' (default)) or a corner placement ('top_left', 'top_right', 'bottom_left', 'bottom_right').

legend_cols (int or None, default=None)

Number of columns in the legend.

Added in version 0.12.0.

legend_opts (dict or None, default=None)

Allows setting specific styling options for the legend. They keys should be attributes of the Legend model for Bokeh and keyword arguments of the Axes.legen method for Matplotlib.

Added in version 0.12.0.

legend#

Controls whether and where the legend appears.

  • Set legend=False to hide the legend.

  • Use legend=True (or legend='right') to show it in the default position.

  • Specify one of the cardinal positions 'top', 'bottom', 'left', or 'right'.

  • Choose a corner placement with 'top_left', 'top_right', 'bottom_left', or 'bottom_right'.

import hvplot.pandas  # noqa

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

opts = dict(
    x='flipper_length_mm', y='body_mass_g', by='species',
    frame_width=200, aspect='square'
)
plot1 = df.hvplot.scatter(title="Default: legend=right", **opts)
plot2 = df.hvplot.scatter(legend='left', title="legend=left", **opts)
plot3 = df.hvplot.scatter(legend='top', title="legend=top", **opts)
plot4 = df.hvplot.scatter(legend='bottom', title="legend=bottom", **opts)
plot5 = df.hvplot.scatter(legend='bottom_left', title="legend=bottom_left", **opts)
plot6 = df.hvplot.scatter(legend='bottom_right', title="legend=bottom_right", **opts)
plot7 = df.hvplot.scatter(legend='top_left', title="legend=top_left", **opts)
plot8 = df.hvplot.scatter(legend='top_right', title="legend=top_right", **opts)
plot1 + plot2
plot3 + plot4
plot5 + plot6
plot7 + plot8

legend_cols#

The legend_cols option allows to define the number of columns of the legend grid.

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

df = pd.DataFrame({"y": np.random.random(20), "cat": list(map(chr, range(97, 117)))})

df.hvplot.scatter(by="cat", height=250, legend_cols=3, title="legend_cols=3")

legend_opts#

The legend_opts option allows to customize the legend styling. For the Bokeh plotting backend, the dictionary keys should be properties of its Legend model, such as background_fill_alpha, background_fill_color, etc.

import hvplot.pandas  # noqa

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

df.hvplot.scatter(
    x="bill_length_mm", y="bill_depth_mm", by="species",
    legend_opts={"background_fill_alpha": 0.2, "background_fill_color": "grey", "spacing": 20}
)

For the Matplotlib plotting backend, the keys should be keyword arguments of its Axes.legend method, such as framealpha, facecolor, etc.

import hvplot.pandas  # noqa
hvplot.extension("matplotlib")

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

df.hvplot.scatter(
    x="bill_length_mm", y="bill_depth_mm", by="species",
    legend_opts={"framealpha": 0.2, "facecolor": "grey", "labelspacing": 2}
)

See also

The backend_opts option to customize even more the styling of a plot.

This web page was generated from a Jupyter notebook and not all interactivity will work on this website.