kevin
kevin

Reputation: 151

matplotlib y-scale as log with base e

I am trying to plot a a function which goes as I_threshold=A*e^(T/T_0). so the y-axis are log scaled with base 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

[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.

enter image description here

However, I get the following image when scattering with y-axis in log scale with base e.

enter image description here

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.

enter image description here

Thank you

Upvotes: 0

Views: 5836

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339775

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]

enter image description here

It would then just be a matter of taste whether to show the ticklabels as base e or base 10 ticks, e.g. 1 * 10^2 compared to 1 * 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'). 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.

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.

enter image description here

Upvotes: 1

Related Questions