Advertisement
Mark2020H

Working with Python3 to solve graphical representation of Cosine and Sin graphs

Sep 12th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # MD Harrington  Date 12-09-2024
  3.  
  4. # Facebook https://www.facebook.com/mark.harrington.14289
  5.  
  6. # Instagram https://www.instagram.com/markukh2021/
  7.  
  8. # Homesite (Still  busy with this at present ) https://eliteprojects.x10host.com/
  9.  
  10. # Github   https://github.com/markh2024   && https://github.com/markh2016
  11.  
  12. # Codeshare https://codeshare.io/codes
  13.  
  14.  
  15. #Code below
  16.  
  17. # Using sequential implementation
  18. #**************************************************************************************************#
  19.  
  20. #!/usr/bin/env python3
  21. # MD Harrington  Date 11-09-2024
  22.  
  23. import numpy as np
  24. import matplotlib.pyplot as plt
  25. import matplotlib.animation as animation
  26.  
  27. # Window dimensions
  28. SCREEN_WIDTH = 800
  29. SCREEN_HEIGHT = 800
  30.  
  31. # Parameters for the sine and cosine waves
  32. amplitude = 200
  33. frequency = 0.08 #
  34. speed = 3 # animation speed
  35.  
  36. # We want two full cycles for both sine and cosine waves
  37. num_cycles = 2
  38. x_values = np.linspace(0, num_cycles * (2 * np.pi) / frequency, 1000)  # 1000 points for smoothness
  39.  
  40. # Precompute the sine and cosine values
  41. y_sin_values = amplitude * np.sin(frequency * x_values)
  42. y_cos_values = amplitude * np.cos(frequency * x_values)
  43.  
  44. # Set up the figure and axis
  45. fig, ax = plt.subplots()
  46. ax.set_xlim(0, max(x_values))
  47. ax.set_ylim(-SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2)
  48.  
  49. plt.title(f'SIN, COS, Angular frequency = {frequency}, amplitude = {amplitude} MD Harrington', pad=30)
  50.  
  51. # Line objects for sine and cosine waves
  52. ln_sin, = plt.plot([], [], 'r-', lw=2, label='Sine')  # 'r-' for red line (sine wave)
  53. ln_cos, = plt.plot([], [], 'b-', lw=2, label='Cosine')  # 'b-' for blue line (cosine wave)
  54.  
  55. # Add a legend to differentiate sine and cosine waves
  56. plt.legend()
  57.  
  58. def init():
  59.     """ Initialize the line objects for both sine and cosine waves. """
  60.     ln_sin.set_data([], [])
  61.     ln_cos.set_data([], [])
  62.     return ln_sin, ln_cos
  63.  
  64. def update(frame):
  65.     """ Update the plot with the next segment of both sine and cosine waves. """
  66.     ln_sin.set_data(x_values[:frame], y_sin_values[:frame])
  67.     ln_cos.set_data(x_values[:frame], y_cos_values[:frame])
  68.     return ln_sin, ln_cos
  69.  
  70. # Create the animation
  71. ani = animation.FuncAnimation(fig, update, frames=len(x_values), init_func=init, blit=True, interval=speed, repeat=False)
  72.  
  73. # Display the plot
  74. plt.show()
  75.  
  76. #**************************************************************************************************#
  77. #  Using class implementation
  78.  
  79.  
  80. import numpy as np
  81. import matplotlib.pyplot as plt
  82. import matplotlib.animation as animation
  83.  
  84. class WaveAnimation:
  85.     def __init__(self, amplitude=200, frequency=0.08, speed=3, screen_width=800, screen_height=800, num_cycles=2):
  86.         self.amplitude = amplitude
  87.         self.frequency = frequency
  88.         self.speed = speed
  89.         self.screen_width = screen_width
  90.         self.screen_height = screen_height
  91.         self.num_cycles = num_cycles
  92.         self.x_values = np.linspace(0, num_cycles * (2 * np.pi) / frequency, 1000)
  93.         self.y_sin_values = amplitude * np.sin(frequency * self.x_values)
  94.         self.y_cos_values = amplitude * np.cos(frequency * self.x_values)
  95.        
  96.         self.fig, self.ax = plt.subplots()
  97.         self.ax.set_xlim(0, max(self.x_values))
  98.         self.ax.set_ylim(-screen_height / 2, screen_height / 2)
  99.         plt.title(f'SIN, COS, Angular frequency = {self.frequency}, amplitude = {self.amplitude} MD Harrington', pad=30)
  100.        
  101.         self.ln_sin, = self.ax.plot([], [], 'r-', lw=2, label='Sine')
  102.         self.ln_cos, = self.ax.plot([], [], 'b-', lw=2, label='Cosine')
  103.         plt.legend()
  104.  
  105.     def init(self):
  106.         """Initialize the sine and cosine lines."""
  107.         self.ln_sin.set_data([], [])
  108.         self.ln_cos.set_data([], [])
  109.         return self.ln_sin, self.ln_cos
  110.  
  111.     def update(self, frame):
  112.         """Update the sine and cosine lines for each frame."""
  113.         self.ln_sin.set_data(self.x_values[:frame], self.y_sin_values[:frame])
  114.         self.ln_cos.set_data(self.x_values[:frame], self.y_cos_values[:frame])
  115.         return self.ln_sin, self.ln_cos
  116.  
  117.     def run(self):
  118.         """Run the animation."""
  119.         ani = animation.FuncAnimation(self.fig, self.update, frames=len(self.x_values), init_func=self.init, blit=True, interval=self.speed, repeat=False)
  120.         plt.show()
  121.  
  122. #  Using the class
  123. if __name__ == "__main__":
  124.     wave_animation = WaveAnimation()
  125.     wave_animation.run()
  126.  
  127.  
  128. #**************************************************************************************************#
  129.  
  130.  
  131. Enjoy !!
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement