簡體   English   中英

matplotlib y-scale作為以e為底的對數

[英]matplotlib y-scale as log with base e

我正在嘗試繪制一個函數,其函數為I_threshold = A * e ^(T / T_0)。 因此y軸以e為底進行對數縮放。

我的輸入看起來像這樣

Temp=[10,12.5,15,17.5,20,22.5,25,27.5,30,32.5,35,37.5,40,42.5,45,47.5,50]

和輸出,即I_threshold看起來像這樣

[22.376331312083646, 22.773439481450737, 23.440242034972115, 23.969920199339803, 24.80014584753161, 25.275728442307503, 26.291852943772966, 26.969268640398795, 28.09683889698702, 28.952552190706545, 30.325961112054102, 31.488435380923281, 33.176033568454699, 34.613872631424236, 36.710165595581906, 38.567151879424728, 41.245216030694756]

我試圖定義一個函數,並使用上述方程式返回該函數。 因此,我使用以下代碼分散並繪制了函數。

fig3=plt.figure(3)
ax3=fig3.add_subplot(111)


def efit(T,T_0,A):
    return(A*np.e**(T/T_0))

Temp=[10,12.5,15,17.5,20,22.5,25,27.5,30,32.5,35,37.5,40,42.5,45,47.5,50]   
params_dremp, covariance_dremp= optimize.curve_fit(efit,Temp,I_threshold)


#print(params_dremp[0],params_dremp[1])

majorLocator_2=MultipleLocator(5)
majorFormatter_2=FormatStrFormatter('%.1f')
minorLocator_2=MultipleLocator(1)

#ax3.xaxis.set_major_locator(majorLocator_2)
#ax3.xaxis.set_major_formatter(majorFormatter_2)
#ax3.xaxis.set_minor_locator(minorLocator_2)


locmaj = LogLocator(base=np.e, numticks=1)
ax3.yaxis.set_major_locator(locmaj)
print(I_threshold)
#print(np.size(Temp),np.size(I_threshold))
plt.scatter(Temp,I_threshold)
ax3.set_yscale('log',basey=np.e)
#plt.semilogy(Temp,I_threshold, basey=np.e)

#plt.plot(Temp,fitt(Temp,*params_dremp),'b--')
plt.xlabel('$ T_c \ (^0 C)$')
plt.ylabel('$ I_t \ (mA) $')
#plt.tick_params(axis='y', which='minor')
plt.grid(True,which="major",color='black',ls="-",linewidth=0.5)

當我分散按正常比例繪制數據時。 我得到以下圖像。

在此處輸入圖片說明

但是,當以e為底的對數刻度在y軸上散射時,我得到以下圖像。

在此處輸入圖片說明

在查看圖像時,此圖似乎不是線性的。 有人知道這里出了什么問題嗎?

我的目標是制作一個看起來像這樣的情節。

在此處輸入圖片說明

謝謝

請注意,如果數據在對數刻度上不是線性的,則意味着該數據不正確或產生此期望的模型是錯誤的。 無論如何,這無濟於事。

使用對數刻度以e10為基數並不會改變圖形本身,因為它們僅相差一個常數因子[ Wikipedia ]

在此處輸入圖片說明

然后,將刻度標簽顯示為基數e還是基數10個基數,例如1 * 10^21 * e^2相比,只是一個口味問題。 由於該圖的范圍只有十年的一小部分,因此使用十進制刻度或僅使用正態數似乎更為合適,如下所示。

為了在繪圖上產生對數刻度,可以使用ax.set_yscale('log') 由於此處要繪制的值的范圍相當有限且不超過十年,因此人們可能仍會使用常規的AutoLocator來調整刻度。

import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, ScalarFormatter

Temp=[10,12.5,15,17.5,20,22.5,25,27.5,30,32.5,35,37.5,40,42.5,45,47.5,50]

I_threshold = [22.376331312083646, 22.773439481450737, 23.440242034972115, 
               23.969920199339803, 24.80014584753161, 25.275728442307503, 
               26.291852943772966, 26.969268640398795, 28.09683889698702, 
               28.952552190706545, 30.325961112054102, 31.488435380923281, 
               33.176033568454699, 34.613872631424236, 36.710165595581906, 
               38.567151879424728, 41.245216030694756]

fig, ax = plt.subplots()

ax.set_yscale('log')
ax.yaxis.set_major_locator(AutoLocator())
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.minorticks_off()

ax.scatter(Temp,I_threshold)

plt.xlabel('$ T_c \ (^\circ C)$')
plt.ylabel('$ I_t \ (mA) $')

plt.grid(True,which="major",color='black',ls="-",linewidth=0.5)

plt.show()

這將根據問題得出所需的情節。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM