finds.utils.plots

Convenience functions for data plotting

  • chart types: date axis, time axis, confidence bands, bar, hist, scatter

  • regression diagnostics

Copyright 2023, Terence Lim

MIT License

finds.utils.plots.ColorMap(n, colormap='brg')[source]

Return sampled colors from spectrum of a color map :param n: number of discrete colors :param colormap: matplotlib colormaps, e.g. tab20, viridis, Blues, cool

Returns:

n x 4 2D-array of RGBA values

finds.utils.plots.plot_bands(mean: Series, stderr: Series, width: float = 1.96, x: List[int] = [], ylabel: str = '', xlabel: str = '', c: str = 'b', loc: str = 'best', legend: List[str] = [], ax: Any = None, fontsize: int = 10, title: str = '', hline: List = [], vline: List = [])[source]

Line plot a series with confidence bands

Parameters:
  • mean – Mean values to plot

  • stderr – Stderr values to plot confidence bands

  • width – Multipler on stderr for confidence bands

  • c – Color to fill bands

  • x – X-axis values

  • ylabel – Axis labels

  • xlabel – Axis labels

  • legend – List of legend labels

  • loc – Location to display legend

  • ax – Axis object

  • fontsize – Base font size

  • title – Main title string

  • hline – List of y-axis values to plot horizontal lines

  • vline – List of x-axis values to plot vertical lines

finds.utils.plots.plot_bar(y: DataFrame, ax: Any = None, labels: List[str] = [], xlabel: str = '', ylabel: str = '', fontsize: int = 12, title: str = '', legend: List[str] = [], loc: str = 'best', alpha: float = 0.8, labelsize: int = 8, rotation: float = 0.0)[source]

Bar plot with annotated points

Parameters:
  • y – DataFrame of y-values, observations in rows, variables in columns

  • ax – Axis object

  • labels – List of labels to annotate

  • labelsize – Font size of annotation labels text

  • rotation – Rotate annotation labels text

  • xlabel – Axis labels

  • ylabel – Axis labels

  • fontsize – Base font size

  • alpha – Transparency of bars

  • title – Main title text

  • legend – List of legend names

  • loc – Location for legend

finds.utils.plots.plot_date(y1: DataFrame, y2: DataFrame | None = None, ax: Any = None, xmin: int = 0, xmax: int = 99999999, cn: int = 0, fontsize: int = 12, rescale: bool = False, yscale: bool = False, ls: str = '-', marker: str | None = '', ms: int | None = None, hlines: List[float] = [], vlines: List[int] = [], nbins: int = 0, vspans: List[Tuple[int, int]] = [], xlabel: str = '', points: DataFrame | Series | None = None, rotation: float = 0, title: str = '', ylabel1: str = '', ylabel2: str = '', legend1: List[str] = [], legend2: List[str] = [], loc1: str = 'upper left', loc2: str = 'upper right')[source]

Line plot with int date on x-axis, and primary and secondary y frames

Parameters:
  • y1 – Plot on primary y-axis

  • y2 – Plot on secondary y-axis

  • ax – Matplotlib axes object from plt.subplots() or plt.gca()

  • cn – Starting CN color to cycle through

  • marker – Marker style, None to cycle (default ‘’ means no marker)

  • xmin – Minimum of x-axis date range

  • xmax – Maximum of x-axis date range (default is auto)

  • nbins – Number of bins for xticks placement (default 0 is auto)

  • rotation – Rotation of x-axis ticks

  • hlines – Y-axis points where to place horizontal lines

  • vlines – X-axis points where to place vertical lines

  • vspans – Vertical regions to highlight

  • xlabel – X-axis label

  • ylabel1 – Y-axis labels

  • ylabel2 – Y-axis labels

  • fontsize – Base font size

  • points – Points and labels to annotate

  • title – Main title

  • legend1 – Lists of legend labels

  • legend2 – Lists of legend labels

  • loc1 – Locations to place legends

  • loc2 – Locations to place legends

  • ls – Linestyle

  • ms – Marker size

finds.utils.plots.plot_fitted(fitted: Series, resid: Series, n: int = 3, ax: Any = None, title: str = 'Residuals vs Fitted', fontsize: int = 12, strftime: str = '%Y-%m-%d') Series[source]

Plot residuals and identify outliers

Parameters:
  • ax – Axis object

  • fitted – Fitted Series

  • residual – Residual Series

  • n – Number of outlier points in each end to identify

  • strftime – string to format time display

  • title – Main title text

  • fontsize – Base font size

finds.utils.plots.plot_groupbar(df: DataFrame, ax: Any = None, labels: DataFrame | None = None)[source]

Plot a grouped bar chart

Parameters:
  • df – DataFrame to plot

  • ax – Axis object

  • labels – optional DataFrame of annotations

finds.utils.plots.plot_hist(*args, kde: bool = True, hist: bool = False, bins: ~typing.List[float] = [], pdf: ~typing.Callable | ~typing.List | ~typing.Dict = <bound method rv_continuous.pdf of <scipy.stats._continuous_distns.norm_gen object>>, ax: ~typing.Any = None, title: str = '', xlabel: str = '', ylabel: str = 'density', fontsize: int = 12)[source]

Histogram bar plot with a benchmark probability density

Parameters:
  • ax – Axis object

  • bins – List of bin values

  • hist – Plots histogram

  • kde – Plots kernel density curve

  • pdf – Benchmark probability density

  • ylabel – Axis labels

  • xlabel – Axis labels

  • fontsize – Base font size

  • title – Main title text

finds.utils.plots.plot_leverage(resid: Series, hat: array, dist: array, ddof: int, title: str = 'Residuals vs Leverage', ax: Any = None, strftime='%Y-%m-%d') DataFrame[source]

Plot leverage and identify influential points

Parameters:
  • resid – Residual Series

  • hat – Hat values

  • dist – Distance values

  • ddof – Degrees of freedom of model

  • ax – Axis object

  • title – Main title text

  • strftime – string to format time display

finds.utils.plots.plot_qq(resid: Series, title: str = 'Normal Q-Q', ax: Any = None, z: float = 2.807, strftime: str = '%Y-%m-%d') DataFrame[source]

QQ Plot

Parameters:
  • resid – Residual Series

  • strftime – string to format time display

  • title – Main title text

  • z – Z-value to identify outliers

finds.utils.plots.plot_scale(fitted: Series, resid: Series, ax: Any = None, title: str = 'Scale-Location', n: int = 3, alpha: float = 0.5, strftime: str = '%Y-%m-%d')[source]

Plot scale of residuals with outliers

Parameters:
  • fitted – Fitted Series

  • resid – Residual Series

  • ax – Axis object

  • title – Main title text

  • alpha – Transparency of points

  • n – number of outliers in each end to identify

  • strftime – string to format time display

finds.utils.plots.plot_scatter(x: Series, y: Series, labels: List = [], ax: Any = None, xlabel: str = '', ylabel: str = '', c: Any = None, cmap: Any = None, alpha: float = 0.75, edgecolor: Any = None, s: float = 10, marker: str = 'o', title: str = '', abline: bool | None = True, fontsize: int = 12)[source]

Scatter plot, optionally with abline slope and point labels

Parameters:
  • x – Series to plot on horizontal axis

  • y – Series to plot on horizontal axis

  • labels – List of annotations for points

  • ax – Matplotlib axes object, from plt.subplots() or plt.gca()

  • xlabel – Horizontal axis label

  • ylabel – Vertical axis label

  • title – Title of plot

  • abline – To plot abline or 45-degree line. If none, do not plot slope

  • labels – List of 3-tuples (text, x, y) to annotate

  • alpha – transparency of scatter points

  • edgecolor – edge color of scatter points

  • marker – marker type of scatter points

  • s – Marker area size

  • cmap – Color map to use for scatter points

  • abline – True for fitted slope, False for 45-degree line, None is no plot

  • fontsize – Base font size

  • title – Main title string

finds.utils.plots.plot_time(y1: DataFrame, y2: DataFrame | None = None, ax: Any = None, xmin: Timestamp = Timestamp('1900-01-01 09:30:00'), xmax: Timestamp = Timestamp('1900-01-01 16:00:00'), title: str = '', marker: str = ' ', fontsize: int = 8)[source]

Plot lines with Timestamp time on x-axis; primary and secondary y-axis

Parameters:
  • y1 – DataFrame to plot on left axis

  • y2 – DataFrame (or None) to plot on right axis

  • ax – Axes object to plot in

  • xmin – Left-most x-axis time, None to include all

  • xmax – Right-most x-axis time, None to include all

  • marker – Style of marker to plot

  • fontsize – Font size of tick labels

finds.utils.plots.set_xticks(ax: Axes, nbins: int = 0, nskip: int = 0, **kwargs)[source]

Set locations for xticks

Parameters:
  • ax – Matplotlib axes object from plt.subplots() or plt.gca()

  • nbins – Number of bins for major ticks, or

  • nskip – Number of ticks to skip (ignore if nbins > 0)

  • **kwargs – Arguments for tick_params(), e.g. labelsize, colors, rotation

finds.utils.plots.subplots(nfigs: int = 1, ncols: int = 1, nrows: int = 1, squeeze: bool = False, **kwargs) List[Axes][source]

Wrapper over plt.subplots to create subplots across multiple figures

Parameters:
  • nfigs – Number of figures

  • ncols – Number of columns per figure

  • nrows – Number of rows per figure

  • squeeze – ignored, will always return 1D flattened list of axes unless only 1

  • **kwargs – other keyword arguments passed on to plt.subplots

Returns:

Tuple of (List of figures, List of axes)