[英]Python/Matplotlib - How to put text in the corner of equal aspect figure
[英]How to put text in figure using matplotlib in python
我正在嘗試在python3
中使用matplotlib
循環 plot 多個數字。 對於循環中的每個元素,我繪制實際曲線和擬合曲線。 這是我編寫的代碼:
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()
我試圖將R-squared, Decay value and several other things
放在圖中,以便於可視化。 對於某些地塊 alignment 很好,但對於其他地塊,文本超出 plot 區域。 我為不同的迭代運行相同的代碼。 如何解決?
這里是情節:
您使用具有固定數字的plt.text
位置值,但您的軸范圍正在改變。 一種選擇是 plot 位置基於您的數據,例如曲線上的最后一個[-1]
或中間[int(t.shape[0]/2.)]
元素,使用移位tshift
偏移文本,例如
plt.text(t[-1]+tshift, y[-1]+tshift,"R-sq:"+str(round(r_squared,2)),fontsize=8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.