Reputation: 579
I have a script that produces data and then I use a least-squares fitter to extract the parameters back from my data. I use this approach to see how those extracted params will vary if I change a certain aspect of my code in a loop. I am using an animation to save this process graphically.
The problem is I want my frames to show that parameter that's being varied. I'm using funcAnimation from the matplotlib.animation library and I have the following update function:
# 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()
This all works just great but I'm wondering how I can use the methods in the objects a,b,c,d that have the following instantiation in order to update the actual figure and not just the data arrays.:
# 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)
If I could somehow update the suptitle within fig on every function call, that would be great!
Upvotes: 0
Views: 515
Reputation: 15017
Use the format method:
figure.suptitle("The current value of x is {}".format(x))
Upvotes: 1