How Do I Update A Matplotlib Figure While Fitting A Function?
I have written a python script which opens UV-Vis spectra and attempts to fit them with a large sum of functions. However, I would like the fitting steps to be shown in a plot as
Solution 1:
interesting, I tried running a simple test.
import time
from matplotlib import pyplot as pltlib
deconvFig = pltlib.figure(2)
ax = deconvFig.add_subplot(111)
X, Y = range(10), range(10)
line1,line2 = ax.plot(X,Y,'r-',X,Y,'r-')
for x in xrange(2, 6, 1):
line2.set_ydata(range(0, 10*x, x))
deconvFig.canvas.draw()
time.sleep(2)
>>> import matplotlib
>>> matplotlib.__version__
'1.1.0'
and well it worked as expected. maybe because your generating a second figure.
import time
from matplotlib import pyplot as pltlib
pltlib.ion()
pltlib.interactive(True)
pltlib.figure(1)
pltlib.plot(range(10),range(10), "r-")
pltlib.show()
deconvFig = pltlib.figure(2)
ax = deconvFig.add_subplot(111)
X, Y = range(10), range(10)
line1,line2 = ax.plot(X,Y,'r-',X,Y,'r-')
for x in xrange(2, 6, 1):
line2.set_ydata(range(0, 10*x, x))
deconvFig.canvas.draw()
time.sleep(2)
nope still worked fine. It could be my setup.
Though its also possible that its minimizing at very slow rate, so when you plot the update you can't tell the difference, you can calculate the RMSE to see how big the difference is
print numpy.sqrt(numpy.sum((data - model)**2)/model.shape[0])/numpy.mean(data) * 100
Also I usually use scipy's minimization function http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html being that it can minimize most functions, though it works by randomly mutating the input so I don't know how fast it can be, but it can be applied in many many situations.
I hope this helps.
Post a Comment for "How Do I Update A Matplotlib Figure While Fitting A Function?"