How to use numpy logspace

I will explain how to use numpy logspace, a handy function for creating logarithmically spaced arrays.

Numpy logspace is a function that returns an array of numbers that are evenly spaced on a log scale. The syntax of the function is:

numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)

The parameters are:

– start: the starting value of the sequence, in log scale
– stop: the final value of the sequence, in log scale
– num: the number of samples to generate, default is 50
– endpoint: whether to include the stop value in the output, default is True
– base: the base of the log scale, default is 10
– dtype: the data type of the output array, default is None
– axis: the axis along which to generate the samples, default is 0

See also  How to generate distribution plot the easiest way in Python?

For example, if we want to create an array of 10 numbers between 10^1 and 10^3 on a log scale, we can use:

import numpy as np
arr = np.logspace(1, 3, num=10)
print(arr)

The output will be:

numpy logspace

We can see that the numbers are not evenly spaced in linear scale, but they are in log scale:

import numpy as np
import matplotlib.pyplot as plt
arr = np.logspace(1, 3, num=10)

plt.semilogx(arr)
plt.show()

numpy logspace plot