code: def plt_longterm_ts_agg(
      df,
      time_col,
      window_col,
      value_col,
      color_col=None,
      agg_func=np.nanmean,
      plt_title="",
      color="blue",
      choose_color_func=None,
  ):
      """Make a longterm avg plot by taking the average in a window
          and moving that across the time.
          That window can be a week, month, year etc
  
      :param df: pd.DataFrame
          The data frame with the data
      :param time_col: str
          The column with timestamps
      :param window_col: str
          This is a column which represents a coarse time granularity
          eg "2018-01" represents year-month
          In this case the avg across that month is calculated
      :param value_col: str
          The column denoting the values
      :param color_col: Optional[str]
          The column denoting the color for each point.
          We allow the curve color to change.
          When aggregating for each window the first color
          appearing in that window will be used by default.
      :param agg_func: Optional[func]
          the aggregation function to be used across the window
      :param plt_title: Optional[str]
          Plot title
      :param color: Optional[str]
          Color of the curve if it is not provided by `color_col`
          in original dataframe
      :param choose_color_func: func
          Function to determine how pointwise colors are translated to aggregated
          to be used for the aggregate curve.
          The default choosed the first color appearing in the data for
          the corresponding slice.
      :return pd.DataFrame
          The aggregated data frame constructed for the plot
      """
      df = df.copy()
  
      # this is for choosing the color for the aggregated curve
      # after averaging
      if choose_color_func is None:
  
          def choose_color_func(color_values):
              return color_values.iloc[0]
  
      if color_col is None:
          color_col = "curve_color"
          df["curve_color"] = color
  
      agg_dict = {
          time_col: np.nanmean,
          value_col: agg_func,
          color_col: choose_color_func,
      }
  
      g = df.groupby([window_col], as_index=False)
      df_agg = g.agg(agg_dict)
  
      df_agg.columns = [window_col, time_col, value_col, color_col]
      df_agg.sort_values(by=time_col, inplace=True)
      # plotting the whole curve in grey
      plt.plot(
          df_agg[time_col], df_agg[value_col], alpha=0.5, color="grey"
      )
      plt.xlabel(time_col)
      plt.ylabel(value_col)
      colors_set = set(list(df[color_col].values))
      # adding scatter points in colors for each part of curve
      # based on `color_col`
      for c in colors_set:
          df_agg_subset = df_agg.loc[
              df_agg[color_col] == c
          ].reset_index(drop=True)
          plt.scatter(
              df_agg_subset[time_col].values,
              df_agg_subset[value_col].values,
              alpha=0.5,
              color=c,
          )
      plt.title(plt_title)
  
      return df_agg
  
file_path: greykite/greykite/common/viz/timeseries_plotting_mpl.py
imports: import colorsys
  import matplotlib
  import numpy
  from matplotlib import pyplot
  from pandas.plotting import register_matplotlib_converters
label_desc: Use image node_id 4 for calling a global function with example usage: plt_longterm_ts_agg(df, time_col, window_col, value_col, color_col, agg_func, plt_title, color, choose_color_func) and returns: df_agg, color_values