Python code to make a Point move in an elliptical path in Python, using matplotlib

Publié le: 27 janvier 2023
sur la chaîne: badr mouazen
117
0

-*- coding: utf-8 -*-
"""
Created on Fri Jan 27 09:35:58 2023

@author: mouaz
"""

-*- coding: utf-8 -*-
"""
Created on Fri Jan 27 09:06:48 2023

@author: mouazen
"""

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from matplotlib.widgets import Button

a = 5
b = 3

K = 1
beta = 1/3

T = 10
N = 1000

t = np.linspace(0, T, N)
R = np.sqrt(a**2*b**2 / ((a*np.sin(t))**2 + (b*np.cos(t))**2))
v = K * R**beta
x = a * np.cos(t)
y = b * np.sin(t)

x_point = x[0]
y_point = y[0]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, '-b', lw=2)
point, = ax.plot(x_point, y_point, 'or')

initialize the speed of the point
speed = 1
flag to check if the animation is running or not
is_running = True

callback function to handle key events
def on_key(event):
global speed, is_running
if event.key == 'up':
speed += 1
elif event.key == 'down':
speed = max(speed-1, 1)
elif event.key == ' ':
is_running = not is_running

callback function to handle mouse events
def on_scroll(event):
global speed
if event.button == 'up':
speed += 1
elif event.button == 'down':
speed = max(speed-1, 1)

callback function for the "+" button
def on_plus_button(event):
global speed
speed += 1

callback function for the "-" button
def on_minus_button(event):
global speed
speed = max(speed-1, 1)

create the buttons
plus_button_ax = plt.axes([0.8, 0.05, 0.1, 0.075])
minus_button_ax = plt.axes([0.7, 0.05, 0.1, 0.075])
plus_button = Button(plus_button_ax, "+")
minus_button = Button(minus_button_ax, "-")

plus_button.on_clicked(on_plus_button)
minus_button.on_clicked(on_minus_button)

connect the callback functions to the figure
fig.canvas.mpl_connect('key_press_event', on_key)
fig.canvas.mpl_connect('scroll_event', on_scroll)

i = 0
def animate(frame):
global i
if is_running:
i += speed
i = i % N
point.set_data(x[i], y[i])
return point,

ani = animation.FuncAnimation(fig, animate, frames=N, interval=T*1000/N)
plt.show()


Sur cette page du site, vous pouvez voir la vidéo en ligne Python code to make a Point move in an elliptical path in Python, using matplotlib durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur badr mouazen 27 janvier 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 117 fois et il a aimé 0 téléspectateurs. Bon visionnage!