I am trying to plot multiple figures in a loop using matplotlib in python3. For every element in loop I draw the actual curve and Fitted curve. Here is the code that I have written:
plt.figure()
plt.plot(t, y,'g--',label="data")
plt.ylabel("Rate")
plt.xlabel("Time")
plt.title("{},{},{}".format(a,b,c),fontsize=8)
plt.text(0.8,0.8,"R-sq:"+str(round(r_squared,2)),fontsize=8)
plt.text(0.8,0.7,"decay:"+str(round(1/(12*fit_b),2)),fontsize=8)
plt.text(40.1,1.0,"#Cohort:"+str(C),fontsize=8)
plt.text(40.1,0.9,"Samples:"+str(int(S)),fontsize=8)
plt.grid()
plt.plot(t, yhat, 'b--',label='Fitted Function:\n $y = %0.4f e^{%0.4f t} $' % (fit_a, fit_b))
plt.legend(bbox_to_anchor=(0.4, 0.4), fancybox=True, shadow=True) #1.4, 1.1
name="./fig_weight/{}__{}__{}.png".format(a,b,c)
plt.savefig(name)
plt.show()
I am trying to put R-squared, Decay value and several other things in figure so that it is easy to visualize. For some of the plots alignment is fine but for others the text goes outside plot area.
I am running the same code for different iteration.
How to fix it?
Here are the plots:

