Skip to content

# viz.py

# `` Viz

    Viz(dpi: int = 100, font_size: int = 11)

Provide a visualization base class.

Parameters:

- ## `dpi`

  (`int`, default: `100` ) –

  resolution for rasterized images

- ## `font_size`

  (`int`, default: `11` ) –

  general font size

Attributes:

- `dpi` (`int`) –

  resolution for rasterized images

- `font_size` (`int`) –

  general font size

- `fdict` (`dict[Any, Any]`) –

  dictionary to which each figure is appended as it is generated

- `colors` (`Callable`) –

  list of colors

- `n_colors` (`int`) –

  number of colors

- `color_cycle` (`Callable`) –

  color property cycle

- `markers` (`tuple`) –

  list of markers

- `n_markers` (`int`) –

  number of markers

- `marker_cycle` (`cycle`) –

  cycle of markers

- `linestyle_list` (`tuple`) –

  list of line styles (solid, dashdot, dashed, custom dashed)

- `color` (`Callable`) –

  return i^th color

- `marker` (`Callable`) –

  return i^th marker

Methods:

- `create_figure` –

  Initialize a `MatPlotLib` figure.

- `get_aspect` –

  Get aspect ratio of graph.

- `get_fonts` –

  Fetch the names of all the font families available on the system.

- `naturalize` –

  Adjust graph aspect ratio into 'natural' ratio.

- `stretch` –

  Stretch graph axes by respective factors.

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    def __init__(self, dpi: int = 100, font_size: int = 11) -> None:
        """Initialize."""
        self.dpi = dpi
        self.font_size = font_size
        self.fdict = {}
        prop_cycle = plt.rcParams["axes.prop_cycle"]
        self.colors = prop_cycle.by_key()["color"]  # type: ignore
        self.n_colors = len(self.colors)  # type: ignore
        self.color_cycle = cycle(self.colors)  # type: ignore
        self.markers = ("o", "s", "v", "p", "*", "D", "X", "^", "h", "P")
        self.n_markers = len(self.markers)
        self.marker_cycle = cycle(self.markers)
        self.linestyle_list = ("solid", "dashdot", "dashed", (0, (3, 1, 1, 1)))

        color_ = lambda i_: self.colors[i_ % self.n_colors]  # type: ignore
        marker_ = lambda i_: self.markers[i_ % self.n_markers]  # type: ignore
        self.color = color_  # type: ignore
        self.marker = marker_  # type: ignore
        font_size = 11
        fonts = self.get_fonts()
        if "Arial" in fonts:
            self.font_family="Arial"
        elif "DejaVu Sans" in fonts:
            self.font_family="DejaVu Sans"
        else:
            self.font_family="Helvetica"
        mpl.rc("font", size=font_size, family=self.font_family)

## `` create_figure

    create_figure(
        fig_name: str, fig_size: tuple[float, float] | None = None, dpi: int | None = None
    ) -> Figure

Initialize a `MatPlotLib` figure.

Set its size and dpi, set the font size, choose the Arial font family if possible, and append it to the figures dictionary.

Parameters:

- ### `fig_name`

  (`str`) –

  name of figure; used as key in figures dictionary

- ### `fig_size`

  (`tuple[float, float] | None`, default: `None` ) –

  optional width and height of figure in inches

- ### `dpi`

  (`int | None`, default: `None` ) –

  rasterization resolution

Returns:

- `Figure` –

  reference to figure

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    def create_figure(
        self,
        fig_name: str,
        fig_size: tuple[float, float] | None = None,
        dpi: int | None = None,
    ) -> Figure:
        """
        Initialize a `MatPlotLib` figure.

        Set its size and dpi, set the font size,
        choose the Arial font family if possible,
        and append it to the figures dictionary.

        Args:
            fig_name:
                name of figure; used as key in figures dictionary
            fig_size:
                optional width and height of figure in inches
            dpi:
                rasterization resolution

        Returns:
            reference to figure
        """
        fig_size_: tuple[float, float] = (
            (8, 8) if fig_size is None else fig_size
        )
        dpi_: float = self.dpi if dpi is None else dpi
        logging.info(
            "gmplib.plot.GraphingBase:\n   "
            + f"Creating plot: {fig_name} size={fig_size_} @ {dpi_} dpi"
        )
        fig = plt.figure()
        self.fdict.update({fig_name: fig})
        if fig_size_ is not None:
            fig.set_size_inches(*fig_size_)
        fig.set_dpi(dpi_)
        return fig

## `` get_aspect

    get_aspect(axes: Axes) -> float

Get aspect ratio of graph.

Parameters:

- ### `axes`

  (`Axes`) –

  the `axes` object of the figure

Returns:

- `float` –

  aspect ratio

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    def get_aspect(self, axes: Axes) -> float: #type: ignore
        """
        Get aspect ratio of graph.

        Args:
            axes:
                the `axes` object of the figure

        Returns:
            aspect ratio
        """
        # Total figure size
        figWH: tuple[float, float] \
            = tuple(axes.get_figure().get_size_inches())  #type: ignore
        figW, figH = figWH
        # Axis size on figure
        bounds: tuple[float, float, float, float] = axes.get_position().bounds
        _, _, w, h = bounds
        # Ratio of display units
        disp_ratio: float = (figH * h) / (figW * w)
        # Ratio of data units
        # Negative over negative because of the order of subtraction
        # logging.info(axes.get_ylim(),axes.get_xlim())
        data_ratio: float = op.sub(*axes.get_ylim()) / op.sub(*axes.get_xlim())
        aspect_ratio: float = disp_ratio / data_ratio
        return aspect_ratio

## `` get_fonts

    get_fonts() -> List[str]

Fetch the names of all the font families available on the system.

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    def get_fonts(self) -> List[str]:
        """Fetch the names of all the font families available on the system."""
        fpaths = matplotlib.font_manager.findSystemFonts()
        fonts: list[str] = []
        for fpath in fpaths:
            try:
                font = matplotlib.font_manager.get_font(fpath).family_name
                fonts.append(font)
            except RuntimeError as re:
                logging.debug(f"{re}: failed to get font name for {fpath}")
                pass
        return fonts

## `` naturalize

    naturalize(fig: Figure) -> None

Adjust graph aspect ratio into 'natural' ratio.

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

    177
    178
    179
    180
    181
    182
    def naturalize(self, fig: Figure) -> None:
        """Adjust graph aspect ratio into 'natural' ratio."""
        axes: Axes = fig.gca() #type: ignore
        # x_lim, y_lim = axes.get_xlim(), axes.get_ylim()
        # axes.set_aspect((y_lim[1]-y_lim[0])/(x_lim[1]-x_lim[0]))
        axes.set_aspect(1 / self.get_aspect(axes))

## `` stretch

    stretch(
        fig: Figure,
        xs: tuple[float, float] | None = None,
        ys: tuple[float, float] | None = None,
    ) -> None

Stretch graph axes by respective factors.

Source code in `.venv/lib/python3.14/site-packages/langevin/base/viz.py`

    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    def stretch(
        self,
        fig: Figure,
        xs: tuple[float, float] | None = None,
        ys: tuple[float, float] | None = None,
    ) -> None:
        """Stretch graph axes by respective factors."""
        axes: Axes = fig.gca() #type: ignore
        if xs is not None:
            x_lim = axes.get_xlim()
            x_range = x_lim[1] - x_lim[0]
            axes.set_xlim(
                x_lim[0] - x_range * xs[0], x_lim[1] + x_range * xs[1]
            )
        if ys is not None:
            y_lim = axes.get_ylim()
            y_range = y_lim[1] - y_lim[0]
            axes.set_ylim(
                y_lim[0] - y_range * ys[0], y_lim[1] + y_range * ys[1]
            )