简体   繁体   English

matplotlib y-scale作为以e为底的对数

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

I am trying to plot aa function which goes as I_threshold=A*e^(T/T_0). 我正在尝试绘制一个函数,其函数为I_threshold = A * e ^(T / T_0)。 so the y-axis are log scaled with base e. 因此y轴以e为底进行对数缩放。

My input look like this 我的输入看起来像这样

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]

and output, which is I_threshold looks like this 和输出,即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]

I tried to def a function and return the function with the equation described above. 我试图定义一个函数,并使用上述方程式返回该函数。 And so I used the following code to scatter and plot the function. 因此,我使用以下代码分散并绘制了函数。

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)

when i scatter plot the data in normal scale. 当我分散按正常比例绘制数据时。 i get the following image. 我得到以下图像。

在此处输入图片说明

However, I get the following image when scattering with y-axis in log scale with base e. 但是,当以e为底的对数刻度在y轴上散射时,我得到以下图像。

在此处输入图片说明

This graph does not seem to be linear when looking at the image. 在查看图像时,此图似乎不是线性的。 Does someone know what went wrong here. 有人知道这里出了什么问题吗?

MY goal is make a plot which looks like this. 我的目标是制作一个看起来像这样的情节。

在此处输入图片说明

Thank you 谢谢

Note that if the data is not linear on a logarithmic scale that means that either the data is not correct or that the model from which this expectation arises is wrong. 请注意,如果数据在对数刻度上不是线性的,则意味着该数据不正确或产生此期望的模型是错误的。 In any case, this is nothing one can help with here. 无论如何,这无济于事。

Whether to use a log scale to base e or 10 does not change the graph itself, because they just differ by a constant factor [ wikipedia ] 使用对数刻度以e10为基数并不会改变图形本身,因为它们仅相差一个常数因子[ Wikipedia ]

在此处输入图片说明

It would then just be a matter of taste whether to show the ticklabels as base e or base 10 ticks, eg 1 * 10^2 compared to 1 * e^2 . 然后,将刻度标签显示为基数e还是基数10个基数,例如1 * 10^21 * e^2相比,只是一个口味问题。 Since the plot in question ranges by only a fraction of a decade, it seems more suitable to use decadic ticks or just normal numbers, as shown below. 由于该图的范围只有十年的一小部分,因此使用十进制刻度或仅使用正态数似乎更为合适,如下所示。

To produce a logarithmic scale on a plot, one may use ax.set_yscale('log') . 为了在绘图上产生对数刻度,可以使用ax.set_yscale('log') Because the range of values to be plotted here is rather limited and does not exceed a decade, one may still use a usual AutoLocator to adjust the ticks. 由于此处要绘制的值的范围相当有限且不超过十年,因此人们可能仍会使用常规的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()

This results in the desired plot from the question. 这将根据问题得出所需的情节。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM