How to create a list of uniformly spaced numbers using a logarithmic scale with Python?

15 Mar, 2021
In this article, we will create a list of uniformly spaced numbers using a logarithmic scale. It means on a log scale difference between two adjacent samples is the same. The goal can be achieved using two different functions from the Python Numpy library.
numpy.logspace: This function returns number scaled evenly on logarithmic scale.
Parameters:
start:  Starting value of sequence is base**start
stop: If endpoint is True then ending value of sequence is base**stop
num (Optional): Specifies the number of samples to generate
endpoint (Optional): It can either be true or false with default value true
base (Optional): Specifies the base of log sequence. Default value is 10.
dtype (Optional): Specifies the type of output array
axis (Optional): The axis in the result to store the samples.
Return: It returns array of samples equally spaced on log scale.
numpy.geomspace: This function is similar to logspace function only difference being end points are specified directly. In Output sample every output is obtained by multiplying previous output by same constant.
Parameters:
start:  It is the starting value of sequence
stop: If endpoint is True then it is the ending value of sequence
num (Optional): Specifies the number of samples to generate
endpoint (Optional): It can either be true or false with default value true
dtype (Optional): Specifies the type of output array
axis (Optional): The axis in the result to store the samples.
Return: It returns array of samples equally spaced on log scale.
Example 1: This example uses logspace function. In this example, start is passed as 1 and the stop is passed as 3 with the base being 10. So starting point of the sequence will be 10**1 = 10 and the ending point of the sequence will be 10**3 = 1000.
Python3
# importing the libraryimport numpy as npimport matplotlib.pyplot as plt  # Initializing variabley = np.ones(10)  # Calculating resultres = np.logspace(1, 3, 10, endpoint = True)  # Printing the resultprint(res)  # Plotting the graphplt.scatter(res, y, color = 'green')plt.title('logarithmically spaced numbers')plt.show()
Output:
Example 2: This example generates the same list as the previous example using geomspace function. Here we directly passed 10 and 1000 as starting and ending points
Python3
# importing the libraryimport numpy as npimport matplotlib.pyplot as plt  # Initializing variabley = np.ones(10)  # Calculating resultres = np.geomspace(10, 1000, 10, endpoint = True)  # Printing the resultprint(res)  # Plotting the graphplt.scatter(res, y, color = 'green')plt.title('logarithmically spaced numbers')plt.show()
Output:
Example 3: In this example, endpoint is set to false so it will generate n+1 sample and return only first n sample i.e. stop will not be included in the sequence.
Python3
# importing the libraryimport numpy as npimport matplotlib.pyplot as plt  # Initializing variabley = np.ones(10)  # Calculating resultres = np.logspace(1, 3, 10, endpoint = False)  # Printing the resultprint(res)
Output:
[ 10.          15.84893192  25.11886432  39.81071706  63.09573445
 100.         158.48931925 251.18864315 398.10717055 630.95734448]
Picked
Python numpy-Mathematical Function
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list