Bonsoir, je me casse la tête depuis quelques heures.
Je me suis inspiré d'un script de démo mais comme je ne comprends pas tout, j'ai du mal à l'adapter.
Je souhaite tracer un graphe avec possibilité de faire varier les paramètres avec des sliders.
Voici mon code :
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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
 
# coefficients stoechiométriques
nu_C3H8 = 1
nu_O2 = 5
nu_CO2 = 3
nu_H2O = 4
 
# Quantités de matières initiales (en mol)
no_C3H8 = 0.1
no_O2 = 0.1
no_CO2 = 0
no_H2O = 0
 
# Avancement maximal
x_max_C3H8 = no_C3H8 / nu_C3H8
x_max_O2 = no_O2 / nu_O2
if x_max_C3H8 < x_max_O2 :
    x_max = x_max_C3H8
else :
    x_max = x_max_O2
 
# Listes de quantités de matière
x = np.arange(0, x_max, x_max/100)
n_C3H8 = no_C3H8-nu_C3H8*x
n_O2 = no_O2 - nu_O2*x
n_CO2 = no_CO2 + nu_CO2*x
n_H2O = no_H2O + nu_H2O*x
 
ax_C3H8 = plt.axes([0.25, 0.1, 0.65, 0.03])
#ax_O2 = plt.axes([0.25, 0.15, 0.65, 0.03])
s_C3H8 = Slider(ax_C3H8, 'Propane', 0, 10.0, valinit=no_C3H8)
#s_O2 = Slider(ax_O2, 'Dioxygène', 0, 10.0, valinit=no_O2)
 
# Courbes
l1, = plt.plot(x, n_C3H8, label="C3H8",lw=2)
l2, = plt.plot(x, n_O2, label="O2")
l3, = plt.plot(x, n_CO2, label="CO2")
l4, = plt.plot(x, n_H2O, label="H2O")
 
liste_des_n_max = [max(n_C3H8), max(n_O2), max(n_CO2), max(n_H2O)]
n_max = 1.1*max(liste_des_n_max)
 
plt.xlabel("Avancement (mol)")
plt.ylabel("Quantité de matière (mol)")
plt.title("Evolution de la réaction de combustion")
plt.legend(loc='upper center')
plt.axis([0,x_max,0,n_max])
plt.grid()
 
def update(val):
#    O2 = s_O2.val
    C3H8 = s_C3H8.val
    l1.set_ydata(C3H8-nu_C3H8*np.arange(0, x_max, x_max/100))
#    l2.set_ydata(O2-nu_O2*np.arange(0, x_max, x_max/100))
#    l3.set_ydata(n_CO2+nu_CO2*np.arange(0, x_max, x_max/100))
#    l4.set_ydata(n_H2O+nu_H2O*np.arange(0, x_max, x_max/100))
    fig.canvas.draw_idle()
 
 
s_C3H8.on_changed(update)
#s_O2.on_changed(update)
 
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
 
def reset(event):
    s_C3H8.reset()
#    s_O2.reset()
button.on_clicked(reset)
 
# Affichage
plt.show()
Il n'y a pas d'erreur mais le graphe est dans le slider, ce qui ne me convient pas...
A mon avis, il y a d'autres problèmes mais tant que j'aurai celui-ci, je ne pourrai pas avancer.
Si quelqu'un connaît ces trucs, je suis preneur.