Search code examples
pythonhvplotholoviz

Show extra columns when hovering in a scatter plot with hvplot


I'm trying to add a label or hover column to the points in a scatter plot, but to no avail.

For use as example data:

import pandas as pd
import holoviews as hv
import hvplot.pandas

df = pd.read_csv('http://assets.holoviews.org/macro.csv', '\t')

df.query("year == 1966").hvplot.scatter(x="gdp", y="unem")

results in the picture below. If I hover over this item I cannot see, which country is represented by the dot (which makes it rather useless). I could use the additional keyword color="country"in the scatterplot call. This would lead to an additional legend (which could be turned off) and the value country: countryname is added to the hover field.

Scatter plot gdp vs. unemployment, country indicator is missing

Is there an option that just adds the column to my hover without adding a legend and changing the color?


Solution

  • You can use keyword hover_cols to add additional columns to your hover.

    Documentation: https://hvplot.holoviz.org/user_guide/Customization.html

    hover_cols (default=[]): list or str
    Additional columns to add to the hover tool or 'all' which will includes all columns (including indexes if use_index is True).


    So in your example if you would like to add specific columns to your hover:

    # use keyword hover_cols to add additional columns when hovering
    df.hvplot.scatter(x="gdp", y="unem", hover_cols=['country', 'year'])
    


    Or if you want to include all additional columns to your hover:

    df.hvplot.scatter(x="gdp", y="unem", hover_cols='all')
    


    Or if you want to include all columns to hover, but not the index:

    df.hvplot.scatter(x="gdp", y="unem", hover_cols='all', use_index=False)