diff --git a/plots/01_tx_lines.py b/plots/01_tx_lines.py index b9ecc7d..2246895 100644 --- a/plots/01_tx_lines.py +++ b/plots/01_tx_lines.py @@ -1,50 +1,16 @@ +# %% imports import logging -from abc import ABC, abstractmethod -from pathlib import Path -from typing import Tuple, Union import numpy as np from matplotlib import pyplot as plt -from matplotlib.animation import FuncAnimation -from matplotlib.axes import Axes -from matplotlib.figure import Figure - -dir_ = Path(__file__).parent.resolve() -dir_root = dir_ / ".." -dir_assets = dir_root / "assets" +from utils import AnimatedPlot, dir_assets +# %% logging log = logging.Logger(__name__) log.setLevel(logging.INFO) -class AnimatedPlot(ABC): - fig: Figure - ax: Union[Axes, Tuple[Axes]] - - def __init__(self, frames: int = 100): - self.frames = int(frames) - self.fig, self.ax = plt.subplots(1, 1) - - def save(self, filename: Union[Path, str], framerate: int = 30): - log.info(f"Generating animation: {self.__class__}...") - an = FuncAnimation( - self.fig, - self.update, - frames=np.linspace(0, 1, self.frames, endpoint=False), - init_func=self.init, - blit=False, - ) - an.save(str(filename), writer="pillow", fps=framerate) - log.info(f"Generating animation: {self.__class__}...Done") - - def init(self) -> None: - pass - - @abstractmethod - def update(self, t: float) -> None: - pass - - +# %% class TxLinePlot(AnimatedPlot): x = np.linspace(-2 * np.pi, 2 * np.pi, 500) @@ -187,5 +153,6 @@ def generate(): ReflectionPlot(zl=1 + 1j).save(dir_assets / "tx_lines" / "reflection_complex.gif") +# %% if __name__ == "__main__": generate() diff --git a/plots/utils.py b/plots/utils.py new file mode 100644 index 0000000..9f4e2ef --- /dev/null +++ b/plots/utils.py @@ -0,0 +1,49 @@ +# %% imports +import logging +from abc import ABC, abstractmethod +from pathlib import Path +from typing import Tuple, Union + +import numpy as np +from matplotlib import pyplot as plt +from matplotlib.animation import FuncAnimation +from matplotlib.axes import Axes +from matplotlib.figure import Figure + +# %% logging +log = logging.Logger(__name__) +log.setLevel(logging.INFO) + +# %% paths +dir_ = Path(__file__).parent.resolve() +dir_root = dir_ / ".." +dir_assets = dir_root / "assets" + + +# %% +class AnimatedPlot(ABC): + fig: Figure + ax: Union[Axes, Tuple[Axes]] + + def __init__(self, frames: int = 100): + self.frames = int(frames) + self.fig, self.ax = plt.subplots(1, 1) + + def save(self, filename: Union[Path, str], framerate: int = 30): + log.info(f"Generating animation: {self.__class__}...") + an = FuncAnimation( + self.fig, + self.update, + frames=np.linspace(0, 1, self.frames, endpoint=False), + init_func=self.init, + blit=False, + ) + an.save(str(filename), writer="pillow", fps=framerate) + log.info(f"Generating animation: {self.__class__}...Done") + + def init(self) -> None: + pass + + @abstractmethod + def update(self, t: float) -> None: + pass