Reputation: 1515
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:
Upvotes: 0
Views: 350
Reputation: 13206
You use plt.text
location values with fixed numbers but your axis range is changing. One option is to plot locations based on your data, for example the last [-1]
or middle [int(t.shape[0]/2.)]
element on your curve with a shift tshift
to offset the text, e.g.
plt.text(t[-1]+tshift, y[-1]+tshift,"R-sq:"+str(round(r_squared,2)),fontsize=8)
Upvotes: 1