How to Overlay Plots#

This guide shows you how to draw several hvPlot plots on the same axes with the * operator.

import holoviews as hv
import hvplot.pandas  # noqa

stocks = hvplot.sampledata.stocks("pandas")

Overlay plots with *#

Multiply plots to draw them on top of each other. Each plot can come from a different data source or use different options.

apple_google = stocks.hvplot.line(x='date', y=['Apple', 'Google'], width=600, height=300)
microsoft_amazon = stocks.hvplot.line(x='date', y=['Microsoft', 'Amazon'])

apple_google * microsoft_amazon

Build an overlay from a list#

When there are many plots to overlay, build them in a list and pass it to hv.Overlay instead of chaining *.

companies = ['Apple', 'Google', 'Microsoft', 'Amazon']
plots = [
    stocks.hvplot.line(x='date', y=company, label=company, ylabel='Price')
    for company in companies
]
hv.Overlay(plots).opts(width=600, height=300, legend_position='top_left')

Overlay different kinds of plots#

The overlaid plots do not need to be of the same kind, for example to highlight the last year on top of a line.

line = stocks.hvplot.line(x='date', y='Apple', width=600, height=300)
last_year = stocks.iloc[-52:].hvplot.area(x='date', y='Apple', alpha=0.4)

line * last_year

Overlay by column values#

To get one overlaid curve per value of a column, use by rather than creating the plots one by one. This requires long data, with the categories in a column.

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

penguins.hvplot.kde('body_mass_g', by='species', alpha=0.5, width=600, height=300)

Next Steps

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