Examples of how to calculate the natural logarithm in python
Calculate the natural logarithm with math
natural logarithm can be calculated using the python module called math:
>>> import math
>>> math.e
2.718281828459045
>>> e = math.e
>>> math.log(e)
1.0
Calculate the natural logarithm with numpy
natural logarithm can also be calculated using numpy:
>>> import numpy as np
>>> a = np.array([e,1,10,e**2])
>>> np.log(a)
array([ 1. , 0. , 2.30258509, 2. ])
Plot the natural logarithm function using matplotlib
import matplotlib.pyplot as plt
import numpy as np
def function(x):
return np.log(x)
x = np.arange(0.01,10,0.1)
y = function(x)
plt.plot(x,y)
plt.grid()
plt.xlim(0,10)
plt.title('How to calculate the Natural logarithm in python ?',fontsize=10)
plt.xlabel('x',fontsize=8)
plt.ylabel('log',fontsize=8)
plt.savefig('how_to_caclulate_natural_logarithm_in_python.png', bbox_inches='tight')
#plt.show()
plt.close()
Logarithm in base 10
>>> math.log10(10)
1.0
>>> math.log10(100)
2.0
>>> math.log10(1000)
3.0
Logarithm in base 2
>>> math.log2(2)
1.0
>>> math.log2(4)
2.0
>>> math.log2(8)
3.0
References
Links | Site |
---|---|
Logarithme naturel | wikipedia |
python numpy ln | stackoverflow |
math log | docs.python |
numpy log | docs.scipy |