mgplot.revision_plot

revision_plot.py Plot ABS revisions to estimates over time.

 1"""
 2revision_plot.py
 3Plot ABS revisions to estimates over time.
 4"""
 5
 6# --- imports
 7from pandas import Series
 8from matplotlib.pyplot import Axes
 9
10
11from mgplot.utilities import annotate_series, check_clean_timeseries
12from mgplot.line_plot import LINE_KW_TYPES, line_plot
13from mgplot.kw_type_checking import validate_kwargs, validate_expected
14from mgplot.kw_type_checking import report_kwargs
15from mgplot.settings import DataT
16from mgplot.kw_type_checking import ExpectedTypeDict
17
18
19# --- constants
20ROUNDING = "rounding"
21REVISION_KW_TYPES: ExpectedTypeDict = {
22    ROUNDING: (int, bool),
23} | LINE_KW_TYPES
24validate_expected(REVISION_KW_TYPES, "revision_plot")
25
26
27# --- functions
28def revision_plot(data: DataT, **kwargs) -> Axes:
29    """
30    Plot the revisions to ABS data.
31
32    Arguments
33    data: pd.DataFrame - the data to plot, the DataFrame has a
34        column for each data revision
35    recent: int - the number of recent data points to plot
36    kwargs : dict :
37        -   units: str - the units for the data (Note: you may need to
38            recalibrate the units for the y-axis)
39        -   rounding: int | bool - if True apply default rounding, otherwise
40            apply int rounding.
41    """
42
43    # --- check the kwargs and data
44    me = "revision_plot"
45    report_kwargs(called_from=me, **kwargs)
46    validate_kwargs(REVISION_KW_TYPES, me, **kwargs)
47
48    data = check_clean_timeseries(data, me)
49
50    # --- critical defaults
51    kwargs["plot_from"] = kwargs.get("plot_from", -19)
52
53    # --- plot
54    axes = line_plot(data, **kwargs)
55
56    # --- Annotate the last value in each series ...
57    rounding: int | bool = kwargs.pop(ROUNDING, True)
58    for c in data.columns:
59        col: Series = data.loc[:, c].dropna()
60        annotate_series(col, axes, color="#222222", rounding=rounding, fontsize="small")
61
62    return axes
ROUNDING = 'rounding'
REVISION_KW_TYPES: mgplot.kw_type_checking.ExpectedTypeDict = {'rounding': (<class 'collections.abc.Sequence'>, (<class 'bool'>, <class 'int'>), <class 'int'>, <class 'bool'>, <class 'NoneType'>), 'ax': (<class 'matplotlib.axes._axes.Axes'>, <class 'NoneType'>), 'style': (<class 'str'>, <class 'collections.abc.Sequence'>, (<class 'str'>,)), 'width': (<class 'float'>, <class 'int'>, <class 'collections.abc.Sequence'>, (<class 'float'>, <class 'int'>)), 'color': (<class 'str'>, <class 'collections.abc.Sequence'>, (<class 'str'>,)), 'alpha': (<class 'float'>, <class 'collections.abc.Sequence'>, (<class 'float'>,)), 'drawstyle': (<class 'str'>, <class 'collections.abc.Sequence'>, (<class 'str'>,), <class 'NoneType'>), 'marker': (<class 'str'>, <class 'collections.abc.Sequence'>, (<class 'str'>,), <class 'NoneType'>), 'markersize': (<class 'float'>, <class 'collections.abc.Sequence'>, (<class 'float'>,), <class 'int'>, <class 'NoneType'>), 'dropna': (<class 'bool'>, <class 'collections.abc.Sequence'>, (<class 'bool'>,)), 'annotate': (<class 'bool'>, <class 'collections.abc.Sequence'>, (<class 'bool'>,)), 'fontsize': (<class 'collections.abc.Sequence'>, (<class 'str'>, <class 'int'>), <class 'str'>, <class 'int'>, <class 'NoneType'>), 'plot_from': (<class 'int'>, <class 'pandas._libs.tslibs.period.Period'>, <class 'NoneType'>), 'legend': (<class 'dict'>, (<class 'str'>, <class 'object'>), <class 'bool'>, <class 'NoneType'>)}
def revision_plot(data: ~DataT, **kwargs) -> matplotlib.axes._axes.Axes:
29def revision_plot(data: DataT, **kwargs) -> Axes:
30    """
31    Plot the revisions to ABS data.
32
33    Arguments
34    data: pd.DataFrame - the data to plot, the DataFrame has a
35        column for each data revision
36    recent: int - the number of recent data points to plot
37    kwargs : dict :
38        -   units: str - the units for the data (Note: you may need to
39            recalibrate the units for the y-axis)
40        -   rounding: int | bool - if True apply default rounding, otherwise
41            apply int rounding.
42    """
43
44    # --- check the kwargs and data
45    me = "revision_plot"
46    report_kwargs(called_from=me, **kwargs)
47    validate_kwargs(REVISION_KW_TYPES, me, **kwargs)
48
49    data = check_clean_timeseries(data, me)
50
51    # --- critical defaults
52    kwargs["plot_from"] = kwargs.get("plot_from", -19)
53
54    # --- plot
55    axes = line_plot(data, **kwargs)
56
57    # --- Annotate the last value in each series ...
58    rounding: int | bool = kwargs.pop(ROUNDING, True)
59    for c in data.columns:
60        col: Series = data.loc[:, c].dropna()
61        annotate_series(col, axes, color="#222222", rounding=rounding, fontsize="small")
62
63    return axes

Plot the revisions to ABS data.

Arguments data: pd.DataFrame - the data to plot, the DataFrame has a column for each data revision recent: int - the number of recent data points to plot kwargs : dict : - units: str - the units for the data (Note: you may need to recalibrate the units for the y-axis) - rounding: int | bool - if True apply default rounding, otherwise apply int rounding.