Advertisement
Korotkodul

draw_waves

Mar 22nd, 2025 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. from functools import partial
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5.  
  6. from IPython.display import HTML
  7. from matplotlib.animation import FuncAnimation
  8. plt.style.use("ggplot")
  9.  
  10.  
  11. figure, axis = plt.subplots(figsize=(9, 9))
  12. axis: plt.Axes
  13. axis.set_xlim(-4.5, 4.5)
  14. axis.set_ylim(-4.5, 4.5)
  15.  
  16. class FrameUpdater:
  17.     def __init__(
  18.         self,
  19.         axis: plt.Axes,
  20.         limit: float,
  21.         frequency: int,
  22.     ) -> None:
  23.         self.axis = axis
  24.         self.limit = limit
  25.         self.frequency = frequency
  26.         self.waves: list[plt.Circle] = []
  27.         self.add_wave()
  28.    
  29.     def __call__(self, frame_id: int) -> list[plt.Circle]:
  30.         if frame_id == 0:
  31.           return self.waves
  32.  
  33.         new_waves = []
  34.         for wave in self.waves:
  35.           wave.set_radius(wave.radius + 0.1)
  36.  
  37.           if wave.radius > self.limit:
  38.             wave.remove()
  39.           else:
  40.             new_waves.append(wave)
  41.  
  42.         self.waves = new_waves
  43.  
  44.         if frame_id % self.frequency == 0:
  45.           self.add_wave()
  46.  
  47.         return self.waves
  48.  
  49.     def add_wave(self):
  50.         wave = plt.Circle((0, 0), 0, fill=False, lw=2, color = 'b')
  51.         self.axis.add_patch(wave)
  52.         self.waves.append(wave)
  53.  
  54. animation = FuncAnimation(
  55.     figure,
  56.     FrameUpdater(
  57.         axis=axis,
  58.         limit=4.5,
  59.         frequency=10,
  60.     ),
  61.     frames=100,
  62.     interval=50,
  63.     blit=True,
  64. )
  65.  
  66. HTML(animation.to_jshtml())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement