Bonsoir

Je souhaite animer une onde avec un slider de matplotlib. Ci-dessous, les deux graphiques et mon code complet.







Voici mon code complet

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 
from matplotlib.widgets import Slider
import numpy as np
import matplotlib.pyplot as plt
 
def f(t, A, T):
 
    return A * np.cos(2*np.pi/T * t)
 
def graphslider():
 
    A = 3
 
    T = 1
 
    t = np.linspace(0,1, 1000)
 
    fig, ax = plt.subplots()
    line = ax.plot(t, f(t, A, T), lw=2)
    ax.grid()
 
    fig.suptitle('Représentation d’une onde avec slider.')
 
    plt.subplots_adjust(bottom=0.25)
 
    axamp = plt.axes([0.25, 0.1, 0.65, 0.03])
 
 
    amp_slider = Slider(ax = axamp,label="Amplitude",valmin=1,valmax=5,valinit=A,orientation="horizontal")
 
 
 
    axfreq = fig.add_axes([0.25, 0.05, 0.65, 0.03])
 
    freq_slider = Slider(ax = axfreq,label="Fréquence",valmin=1,valmax=7.6,valinit=T,orientation="horizontal")
 
 
    def update(val):
 
        line.set_ydata(f(t,amp_slider.val,freq_slider.val))
        fig.canvas.draw_idle()
 
 
    amp_slider.on_changed(update)
 
    freq_slider.on_changed(update)
 
    plt.show()
 
 
def graph():
 
 
    t = np.linspace(0,10, 90)
 
    s = 2 *np.cos(2*np.pi/3 *t)
 
 
    fig = plt.figure(figsize=(16, 8), layout="constrained")
    spec = fig.add_gridspec(2, 2)
 
    ax0 = fig.add_subplot(spec[0, :])
    ax0.plot(t,s)
    ax0.set_title('Amplitudes et Périodes fixe.')
    ax0.grid()
    ax0.set_xlabel('Position')
    ax0.set_ylabel('Amplitude')
 
    ax10 = fig.add_subplot(spec[1, 0])
    ax10 .plot(t,1 *np.cos(2*np.pi/3 *t), label='amplitude = 1')
    ax10 .plot(t,2.2 *np.cos(2*np.pi/3 *t), label='amplitude = 2,2')
    ax10 .plot(t,3.5 *np.cos(2*np.pi/3 *t), label='amplitude = 3,5')
    ax10 .plot(t,5 *np.cos(2*np.pi/3 *t), label='amplitude = 5')
    ax10.set_title('Amplitudes différentes.')
    ax10.grid()
    ax10.set_xlabel('Position')
    ax10.set_ylabel('Amplitude')
    ax10.legend()
 
    ax11 = fig.add_subplot(spec[1, 1])
    ax11.plot(t,2 *np.cos(2*np.pi/3 *t), label='périodes = 3')
    ax11.plot(t,2 *np.cos(2*np.pi/5.2 *t), label='périodes = 5,2')
    ax11.plot(t,2 *np.cos(2*np.pi/7.6 *t), label='périodes = 7,6')
    ax11.grid()
    ax11.set_title('Périodes différentes.')
    ax11.set_xlabel('Position')
    ax11.set_ylabel('Amplitude')
    ax11.legend()
    fig.suptitle('Représentation d’une onde.')
 
    plt.show()
 
 
def afficher():
 
 
    graph()
 
    graphslider()
 
afficher()
Voici l'erreur que python me renvoi.



raceback (most recent call last):
File "C:\Users\EDMOND\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\cbook\__init__.py", line 307, in process
func(*args, **kwargs)
File "C:\Users\EDMOND\AppData\Local\Programs\Python\Python38\lib\site-packages\matplotlib\widgets.py", line 573, in <lambda>
return self._observers.connect('changed', lambda val: func(val))
File "C:\Users\EDMOND\Documents\programme python\TP5exercice2.py", line 48, in update
line.set_ydata(f(t,amp_slider.val,freq_slider.val))
AttributeError: 'list' object has no attribute 'set_ydata'
Si j'ai bien compris l'erreur vient de line.set_ydata(f(t,amp_slider.val,freq_slider.val)).

Est-ce que c'est dû à la position des slider ?

A bientôt