lets reuse some helpers
This commit is contained in:
parent
3922e57e1e
commit
a21849641d
@ -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()
|
||||
|
49
plots/utils.py
Normal file
49
plots/utils.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user