Skip to content

Grid settings

default_plot_axes_in_grid(nrows, ncols, total=None, day_labeler=None, time_labeler=None)

Additional layer on the display_settings_in_grid in order to modify the settings.

Yields:

Type Description
PlotAxes

PlotAxes instance with appropriate display settings based on the position in the grid.

Source code in latent_calendar/plot/grid_settings.py
def default_plot_axes_in_grid(
    nrows: int,
    ncols: int,
    total: int | None = None,
    day_labeler: DayLabeler | None = None,
    time_labeler: TimeLabeler | None = None,
) -> Generator[PlotAxes, None, None]:
    """Additional layer on the display_settings_in_grid in order to modify the settings.

    Yields:
        PlotAxes instance with appropriate display settings based on the position in the grid.

    """
    day_labeler = day_labeler if day_labeler is not None else DayLabeler()
    default_stride = 2 if nrows <= 2 else 4
    time_labeler = (
        time_labeler if time_labeler is not None else TimeLabeler(stride=default_stride)
    )

    for display_settings in display_settings_in_grid(
        nrows=nrows, ncols=ncols, total=total
    ):
        day_labeler.display = display_settings.x
        time_labeler.display = display_settings.y

        yield day_labeler, time_labeler

display_settings_in_grid(nrows, ncols, total=None)

Helper for display logic in a grid.

Can be used with zip since zip function will stop at the shorts of the iterators

Yields:

Type Description
DisplaySettings

DisplaySettings instance with the appropriate settings based on position in the grid.

Source code in latent_calendar/plot/grid_settings.py
def display_settings_in_grid(
    nrows: int,
    ncols: int,
    total: int | None = None,
) -> Generator[DisplaySettings, None, None]:
    """Helper for display logic in a grid.

    Can be used with zip since zip function will stop at the shorts of the iterators

    Yields:
        DisplaySettings instance with the appropriate settings based on position in the grid.

    """
    total = total if total is not None else nrows * ncols

    yield from (
        DisplaySettings(
            x=last_in_column(i, nrows, ncols, total), y=is_left_edge(i, ncols)
        )
        for i in range(total)
    )

get_rows_and_cols(n, max_cols)

Return the number of rows and cols.

Source code in latent_calendar/plot/grid_settings.py
def get_rows_and_cols(n: int, max_cols: int) -> tuple[int, int]:
    """Return the number of rows and cols."""
    nrows = max((n // max_cols) + 1, 1)
    ncols = min(n, max_cols)

    if n % max_cols == 0:
        nrows -= 1

    return nrows, ncols

grid_axes(nrows, ncols, total)

Yields a grid of size nrow, ncols with total cap.

Using this instead of plt.subplots(ncols, nrows) and deleting

Source code in latent_calendar/plot/grid_settings.py
def grid_axes(nrows: int, ncols: int, total: int) -> Generator[plt.Axes, None, None]:
    """Yields a grid of size nrow, ncols with total cap.

    Using this instead of plt.subplots(ncols, nrows) and deleting

    """
    gs = gridspec.GridSpec(nrows, ncols)

    fig = plt.figure()

    yield from (fig.add_subplot(gs[i]) for i in range(total))

Comments