1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, Slider
def display():
# Data
x = np.arange(10)
y = x**2
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
line, = ax.plot([], [])
# Création d'un slider permettant de rafraichir la figure tracée
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
slider = Slider(
ax=ax_slider,
label='',
valmin=0,
valmax=9,
valinit=0,
valstep=1,
)
# Fonction appelée à chaque changement de la variable derrière le slider
def update(i):
line.set_xdata(x[:int(i)])
line.set_ydata(y[:int(i)])
slider.on_changed(update)
# Création d'un bouton permettant de jouer automatiquement la cinématique
ax_play = plt.axes([0.69, 0.025, 0.1, 0.04])
ax_stop = plt.axes([0.80, 0.025, 0.1, 0.04])
play_button = Button(ax_play, 'Play', hovercolor='0.975')
stop_button = Button(ax_stop, 'Stop', hovercolor='0.975')
playing = False
stopping = False
def play(event):
for i in range(10):
slider.set_val(i)
plt.draw()
plt.pause(1)
def stop(event):
stopping = True
play_button.on_clicked(play)
stop_button.on_clicked(stop)
ax.set_xlim((0, 10))
ax.set_ylim((0, 100))
plt.show()
display() |
Partager