Search code examples
pythonholoviewshvplotholoviz

Lineplot with markers in holoviews (or hvplot)


I know how to make a lineplot with holoviews / hvplot.
But how can I add markers of the datapoints to my line plot?

The plot should look something like this:

markers added to lineplot

Here's some sample code:

# import libraries
import numpy as np
import pandas as pd

import hvplot.pandas
import holoviews as hv

# create sample data
df = pd.DataFrame({
    'date_col': pd.date_range(start='1-1-2020', freq='m', periods=12),
    'value_col': np.random.normal(size=12),
})

Solution

  • You can add markers to a line plot by combining it with a scatterplot.

    In Holoviews this is called an overlay and this overlay can be done by using the * symbol.

    The Hvplot solution:

    # create a line plot
    line_plot = df.hvplot.line(
        x='date_col', 
        y='value_col',
    )
    
    # scatter plot with some extra styling options just for fun
    scatter_plot = df.hvplot.scatter(
        x='date_col',
        y='value_col',
    ).opts(color='black', size=20, marker='o')
    
    # overlay the scatterplot on the lineplot to get markers added to your lineplot
    line_plot * scatter_plot
    

    The Holoviews solution:

    # overlay the scatter plot on the curve by using the * symbol
    hv.Curve(df) * hv.Scatter(df).opts(color='black', size=20, marker='o')