fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. # Set up the figure and axis
  6. fig, ax = plt.subplots()
  7. ax.set_xlim(-1.5, 1.5)
  8. ax.set_ylim(-1.5, 1.5)
  9. ax.set_aspect('equal')
  10. ax.axis('off')
  11.  
  12. # Create a circle to represent the structure (e.g., a muscle or valve)
  13. circle = plt.Circle((0, 0), 0.5, color='pink', fill=True)
  14. ax.add_artist(circle)
  15.  
  16. # Create two points to represent fingers
  17. finger1, = ax.plot([], [], 'o', color='red', markersize=10)
  18. finger2, = ax.plot([], [], 'o', color='blue', markersize=10)
  19.  
  20. # Initialize the animation
  21. def init():
  22. finger1.set_data([], [])
  23. finger2.set_data([], [])
  24. return finger1, finger2
  25.  
  26. # Update function for the animation
  27. def update(frame):
  28. # Simulate the movement of two fingers opening and closing
  29. angle = np.radians(frame) # Convert frame to radians
  30. radius = 0.8 # Distance from the center
  31.  
  32. # Position of the first finger (red)
  33. x1 = radius * np.cos(angle)
  34. y1 = radius * np.sin(angle)
  35.  
  36. # Position of the second finger (blue)
  37. x2 = -radius * np.cos(angle)
  38. y2 = -radius * np.sin(angle)
  39.  
  40. # Update the positions of the fingers
  41. finger1.set_data(x1, y1)
  42. finger2.set_data(x2, y2)
  43.  
  44. # Adjust the size of the circle based on the distance between fingers
  45. distance = np.sqrt((x1 - x2)**2 + (y1 - y2)**2)
  46. circle_radius = 0.5 + 0.2 * (1 - distance / (2 * radius)) # Scale the circle
  47. circle.set_radius(circle_radius)
  48.  
  49. return finger1, finger2, circle
  50.  
  51. # Create the animation
  52. ani = FuncAnimation(fig, update, frames=np.linspace(0, 360, 180), init_func=init, blit=True)
  53.  
  54. # Show the animation
  55. plt.show()
Success #stdin #stdout 0.78s 56196KB
stdin
Standard input is empty
stdout
Standard output is empty