update x value using slider matplotlib

匿名 (未验证) 提交于 2019-12-03 01:09:02

问题:

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() 

回答1:

Assuming you have time on the x-axis and want to change the maximum time of your plot that is created by the same function every time, I came up with this:

import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider  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, = ax.plot(t, a, lw=3, color='crimson') plt.axis([0, 20, 0, 1])  axrs = plt.axes([0.25, 0.1, 0.65, 0.03]) srs = Slider(axrs, 'Reaktionsschritte', 0, 20, valinit=0)  def update(x):     t0 = x     t = np.arange(0, t0, .5)     ax.lines.pop(0)  # remove previous line plot     ax.plot(t, np.exp(- k0 * t), lw=3, color='crimson')  # plot new one     fig.canvas.draw()  srs.on_changed(update)  plt.show() 

See what it does when changing the Slider value and let me know if this is what you wanted it to do.


To your edit:
When you add a second plot, you have two lines objects. Try to print ax.lines directly after you run the code (before touching the Slider) and see that it really is a list of two lines. Then call ax.lines.pop(0) and see that one element is popped from the list. That's what the above code does, it removes lines from the axes object ax every time you touch the Slider (because then update is called), which after calling fig.canvas.draw() leads to vanishing of previous plots. If you now touch the Slider once, then two new lines are added to ax and only one is removed. This is why you think there is no going back.
So if you now added a second plot, you just have to pop twice from the list ax.lines with ax.lines.pop(0) and the code works fine ;-)



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!