Search code examples
pythonplotlogarithm

Why only one line goes to zero when several lines are plotted with semilogx function python


I am trying to several logarithimic lines in the one plot with "semilogx fuction" by python. However, only one line goes to zero and other lines don't exactly go to zero even though they should go to zero. The code is:

ustar1 = 0.15
z0=0.0002
z1 = 0.05
z2 = 0.25
z3 = 0.5
z4 = 5.0
k = 0.4

zp1 = np.arange(0.00,300,0.01)
M1 = (ustar1/k)*np.log(zp1/z0)
M2 = (ustar1/k)*(np.log(zp1/z1))
M3 = (ustar1/k)*(np.log(zp1/z2))
M4 = (ustar1/k)*np.log(zp1/z3)
M5 = (ustar1/k)*(np.log(zp1/z4))
plt.semilogx(M1,zp1,'k',label='z0=0.0002')
plt.semilogx(M2,zp1,'b',label='z0=0.05')
plt.semilogx(M3,zp1,'g',label='z0=0.25')
plt.semilogx(M4,zp1,'m',label='z0=0.5')
plt.semilogx(M5,zp1,'r',label='z0=5.0')

As shown aboce, M1 to M5 should go to zero when zp1 equal to z0 to z4 and when I printed them, it did. However when I plotted it only M5 went to zero, other didb't. When I removed plot for M5, the plot for M4 went to zero and others didn't and when removed M5 and M4, M3 did same thing.

I am not sure why this kind of thing happens. Is it because of the semilogx function itself? Anyhelp or advice would be really appreciated. Thank you, Isaac


Solution

  • zp1 = np.arange(0.00, 300, 0.01)
    M1 = (ustar1/k)*np.log(zp1/z0)
    

    You realize you are taking the log(0)? zp1[0] is 0.0, so its log is infinite.

    If you change the range to np.arange(0.01, 300, 0.01) avoid that problem.

    About 'going through 0': When does this curve go through 0? I think there is some confusion

    • You are plotting the results (M1...) on the X axis. Was that the idea? The y values are the z0..z4 you defined, which are all positive.

    • If you swap x, y (Mx, zx) you get:

    enter image description here