I try to draw a graph, which show the progress of a chemical reaction. The progress itself (time or reactionsteps) should be changeable using a slider. The code I have so far:
import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) fig.canvas.set_window_title('Reaktionsfortschritt') t0 = 0 t = np.arange(0, t0, .5) k0 = 0.17 a = np.exp(- k0 * t) l, = plt.plot(t, a, lw=3, color='crimson') plt.axis([0, 20, 0, 1]) axrs = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor='lightblue') srs = Slider(axrs, 'Reaktionsschritte', 0, 20, valinit=0) def xval(*args): x = srs.val t = np.arange(0, x, 0.5) #l.set_ydata(np.exp(- 0.6 * t)) #plt.plot(t, a) fig.canvas.draw_idle() srs.on_changed(xval) plt.show() As far as I understand the plot-range (t) is updated using the xval-function. However, there is no plotting of the graph. I tried both replotting using plt.plot(t, a) as well as l.set_ydata(...).
edited
Ok, so now I added a second function (b) that describes product formation. I added the function also in the same way to the update-function. As a result I get a very strange behaviour: using the slider, I can plot only in positive x-direction, e.g. there I no going back. Once the graph is drawn, it won't 'undraw' when reducing the slider value. Any suggestion why that is?
import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) fig.canvas.set_window_title('Reaktionsfortschritt') t = np.arange(0, 0, .5) k0 = 0.17 a = np.exp(- k0 * t) b = 1 - np.exp(- k0 * t) # plot l, = plt.plot(t, a, lw=3, color='crimson') m, = plt.plot(t, b, lw=3, color='dodgerblue') plt.axis([0, 20, 0, 1]) plt.grid(True) axrs = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor='lightblue') srs = Slider(axrs, 'Zeit', 0, 20, valinit=0) def update(x): t = np.arange(0, x, 2) ax.lines.pop(0) # remove previous line plot ax.plot(t, np.exp(- k0 * t), lw=3, color='crimson') ax.plot(t, 1 - np.exp(- k0 * t), lw=3, color='dodgerblue') fig.canvas.draw() srs.on_changed(update) plt.show()