How to use pn indicator trend

Hi All,

how to use pn indicator trend? for example I have synthetic data showing sales over the month, how I make indicator trend for year on year or montly sales trend

import pandas as pd
import panel as pn

# Sample data (replace this with your actual data)
data = {
    'Date': pd.date_range(start='2020-01-01', periods=12, freq='M'),
    'Sales': [100, 120, 150, 130, 110, 140, 160, 180, 200, 220, 250, 230]
}

df = pd.DataFrame(data)

# Calculate year-over-year sales
df['YoY_Sales'] = df['Sales'].pct_change(periods=12) * 100

# Create the Trend indicator for monthly car sales
trend_indicator = pn.indicators.Trend(df['Sales'], style={'color': '#1f77b4'})

# Create the Trend indicator for year-over-year car sales
yoy_trend_indicator = pn.indicators.Trend(df['YoY_Sales'], style={'color': '#ff7f0e'})

# Create the Panel layout
app_layout = pn.Column(
    trend_indicator,
    yoy_trend_indicator
)

# Display the app
app_layout.servable()

I run that code above and error.

if I just follow example from panel documentation

import pandas as pd
import panel as pn

# Sample data (replace this with your actual data)
data = {
    'Date': pd.date_range(start='2020-01-01', periods=12, freq='M'),
    'Sales': [100, 120, 150, 130, 110, 140, 160, 180, 200, 220, 250, 230]
}

df = pd.DataFrame(data)


trend = pn.indicators.Trend(
    name='Price', data=df, width=200, height=200
)
trend

result wil be zero %

kindly please advise to use this great function.

thanks

Hi @rh1

There are several things that need to change.

  • The YoY_Sales values are NaN in your calculation. Thus the chart will look empty.
  • You need to use named arguments. I.e. value=data.
  • By default the columns 'x' and 'y' will be plotted. You need to specify plot_x and plot_y to change this.
  • Change style to styles for this to work with Panel 1.3.6 or later.
  • For some unknown reason the date_range does not work as x-axis values. Use numbers instead. See #6158 ยท holoviz/panel

The below works

import pandas as pd
import panel as pn
import numpy as np

data = pd.DataFrame(
    {
        "Date": pd.date_range(start="2020-01-01", periods=12, freq="M"),
        "Sales": [100, 120, 150, 130, 110, 140, 160, 180, 200, 220, 250, 230],
    }
)
data["YoY_Sales"] = data["Sales"].pct_change() * 100

# Hack. For some reason the Date column does not work for the x-axis
data["x"] = list(range(len(data["Date"])))

trend_indicator = pn.indicators.Trend(
    data=data,
    plot_x="x",
    plot_y="Sales",
    styles={"color": "#1f77b4"},
    height=200,
    width=400,
)
yoy_trend_indicator = pn.indicators.Trend(
    data=data,
    plot_x="x",
    plot_y="YoY_Sales",
    styles={"color": "#ff7f0e"},
    height=200,
    width=400,
)

app_layout = pn.Column(trend_indicator, yoy_trend_indicator)

app_layout.servable()

image

Hi @Marc perfect ,many thanks