我有一个生成数据的脚本,然后我使用最小二乘拟合器从我的数据中提取参数。我使用这种方法来查看如果我在循环中更改代码的某个方面,那些提取的参数将如何变化。我正在使用动画以图形方式保存此过程。
问题是我希望我的框架显示正在变化的参数。我正在使用matplotlib.animation库中的funcAnimation,我有以下更新函数:
# Update the plots using the following function
def updategal(*args):
global x0_a,x0_b,distance,dev_1,dev_2
x0_a += 0.05; x0_b += -0.05
distance = x0_b - x0_a # arcsec
print "separation = %.2f Arcsec"%distance
# Return the 2D data I need to update the images with imshow.
im, best_fit, residual,diff_e, result_covar = run_2_galaxy_vary_distance(flux_a,HLR_a,e1_a,e2_a,x0_a,y0_a,
flux_b,HLR_b,e1_b,e2_b,x0_b,y0_b,
size,size,pixel_scale,func_gauss,func_gauss,dev_1,dev_2) # If data is no good, continue to re-run
while result_covar is None:
print "Undefined Covariance Matrix\n Rerunning"
im, best_fit, residual,diff_e,result_covar = run_2_galaxy_vary_distance(flux_a,HLR_a,e1_a,e2_a,x0_a,y0_a,
flux_b,HLR_b,e1_b,e2_b,x0_b,y0_b,
size,size,pixel_scale,func_gauss,func_gauss,dev_1,dev_2)
else:
print "result.covar is defined."
error_diag = np.sqrt(np.diag(result_covar))
error_mat = np.outer(error_diag,error_diag)
correlation_mat = result_covar/error_mat
a.set_array(im)
b.set_array(best_fit)
c.set_array(residual)
d.set_array(correlation_mat)
return a,b,c,d
time = 0.2
anim = animation.FuncAnimation(fig, updategal, frames=4, interval=time, blit=True)
anim.save('dist_02.avi', codec='avi', fps=1)
plt.show()
这一切都很好但我想知道如何使用具有以下实例化的对象a,b,c,d中的方法来更新实际数字而不仅仅是数据数组。: / p>
# Provide the fontsize
fonts = 10
# Initialize the geometry of the grid
gs = gridspec.GridSpec(3,5)
# Set the figure size
fig = plt.figure(figsize=(20,11))
plt.suptitle(r'$Image\/Scale:$ $\frac{0.2\rq\rq}{Pixel}$',fontsize=fonts+15)
# Add the first subplot
ax1 = fig.add_subplot(gs[0,0])
plt.title('$Image$',fontsize=fonts+8)
plt.ylabel('$Pixels$',fontsize=fonts+8)
plt.tick_params(axis='both',which='major',labelsize=9)
a = ax1.imshow(im,interpolation='none',origin='lower',vmax=im.max(),vmin=im.min())
plt.colorbar(a,shrink=1)
# Add the second subplot
ax2 = fig.add_subplot(gs[1,0])
plt.ylabel('$Pixels$',fontsize=fonts+8)
plt.tick_params(axis='both',which='major',labelsize=9)
plt.title('$Best Fit$',fontsize=fonts+8)
b = ax2.imshow(best_fit,origin='lower',vmax=im.max(),vmin=im.min())
plt.colorbar(b,shrink=1)
# Add the third subplot
ax3 = fig.add_subplot(gs[2,0])
plt.tick_params(axis='both',which='major',labelsize=9)
plt.title('$Residual$',fontsize=fonts+8)
plt.xlabel('$Pixels$',fontsize=fonts+8)
plt.ylabel('$Pixels$',fontsize=fonts+8)
c = ax3.imshow(residual,interpolation='none',origin='lower')
plt.colorbar(c,shrink=1)
# Add the fourth subplot
ax4 = fig.add_subplot(gs[:,1:])
plt.title('Correlation Coefficient Matrix',fontsize=fonts+8)
d = ax4.imshow(correlation_mat,interpolation='none',origin='lower',vmin=-1,vmax=1)
plt.xticks([0,1,2,3,4,5,6,7,8,9,10,11],['$Flux_a$','$HLR_a$','$e1_a$','$e2_a$','$x0_a$','$y0_a$',
'$Flux_b$','$HLR_b$','$e1_b$','$e2_b$','$x0_b$','$y0_b$'],fontsize=18)
plt.yticks([0,1,2,3,4,5,6,7,8,9,10,11],['$Flux_a$','$HLR_a$','$e1_a$','$e2_a$','$x0_a$','$y0_a$',
'$Flux_b$','$HLR_b$','$e1_b$','$e2_b$','$x0_b$','$y0_b$'],fontsize=18)
plt.colorbar(d,shrink=1)
如果我能以某种方式在每个函数调用中更新fig中的suptitle,那就太棒了!
答案 0 :(得分:1)
使用格式方法:
figure.suptitle("The current value of x is {}".format(x))