4

I am new to hvplot and trying to include a call to .hvplot() inside a function definition, but it's not working. The following code works and displays a figure as expected:

import pandas as pd
import hvplot.pandas

df = pd.DataFrame([1, 5, 3, 4, 2])
df.hvplot()

but if I try something like:

def plot(df):
    df.hvplot()
plot(df)

I get no output. This is in a Jupyter Notebook. What am I missing?

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
Dan
  • 1,105
  • 12
  • 15
  • 1
    The way to think about this is that an hvplot works the same as a string or integer: if you want to display the string or integer in a notebook, you either need to call `display()` on it, or you need to make sure it's returned as the value of the notebook cell (i.e., is the value of the last line of the cell). Same for a .hvplot(); it displays if displayed explicitly or returned as the value of the cell. Here the value of the cell is None, because your function returns None, so nothing is displayed. – James A. Bednar Jan 27 '21 at 01:49

1 Answers1

3

You need to return the result of your function:

def plot(df):
    return df.hvplot()

plot(df)

Or:

def plot(df):
    my_plot = df.hvplot()
    return my_plot

plot(df)
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96