• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:9 mins read
You are currently viewing How to Use NumPy logspace() in Python

Python NumPy logspace() function is used to create an array of evenly spaced values between two numbers on the logarithmic scale. It returns a NumPy array of uniformly spaced values on the log scale between the start and stop. In this article, I will explain NumPy logspace() function syntax and using its parameters how we can return the numbers spaced evenly on a log scale with examples.

Advertisements

1. Quick Examples of Python NumPy logspace() Function

If you are in a hurry, below are some quick examples of how to use logspace() function in Python NumPy.


# Below are the quick examples

# Example 1: Equally spaced values on log scale between 2 and 3
arr = np.logspace(2, 3)

# Example 2: Six equally spaced values on log scale between 2 and 3
arr = np.logspace(2, 3, num=6)

# Example 3: Exclude the stop endpoint
arr = np.logspace(2, 3, num=6, endpoint=False)

# Example 4: Use a different log base for the log scale
arr = np.logspace(2, 3, num=7, base=2)

# Example 5: Graphical representation of numpy.logspace() using matplotlib module
arr = 40
a1 = np.logspace(0.6, 4, arr, endpoint=True)
a2 = np.logspace(0.6, 4, arr, endpoint=False)
b = np.zeros(arr)
plt.plot(a1, b, 'o')
plt.ylim([-0.8, 4])
plt.show()

# Example 6: Graphical representation of numpy.logspace() 
arr = np.logspace(2, 3, num=6)
arr1 = np.zeros(6)
plt.plot(arr, arr1, 'o')

2. Syntax of NumPy logspace()

Following is the syntax of the logspace() function.


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

2.1 Parameters of logspace()

Following are the parameters of logspace().

  • start – The base ** start is the starting value of the sequence. Here base is power of start.
  • stop – The base ** stop is the final value of the sequence unless, the endpoint is False.
  • num – This represents the number of samples to generate. Default is 50.
  • endpoint – It is a boolean value, and if it is True, stop is the last sample. Otherwise, it is not included. Default is True.
  • base – It is a Base of log scale. By default, equals 10.0
  • dtype – The data type of the output array.
  • axis – The axis in the result to store the samples.

2.2 Return Value of logspace()

It returns ndarray – number samples, equally spaced on a log scale.

3. Usage of NumPy logspace()

logspace() function is available in NumPy module package and is used to create an array where the elements are allocated evenly spaced on the log scale. In linear space, the sequence starts at base ** start and ends with base ** stop. These values are equally spaced on the log scale.

The below example creates an array of equally spaced numbers on the log scale between 2 and 3. It returns 50 values in the returned array. In linear space, the sequence starts at base ** start and ends with base ** stop.


import numpy as np
# Equally spaced values on log scale between 2 and 3
arr = np.logspace(2, 3)
print(arr)

Yields below output. By default it returns 50 values.


# Output:
[ 100.          104.81131342  109.8541142   115.13953993  120.67926406
  126.48552169  132.57113656  138.94954944  145.63484775  152.64179672
  159.98587196  167.68329368  175.75106249  184.20699693  193.06977289
  202.35896477  212.09508879  222.29964825  232.99518105  244.20530945
  255.95479227  268.26957953  281.1768698   294.70517026  308.88435965
  323.74575428  339.32217719  355.64803062  372.75937203  390.69399371
  409.49150624  429.19342601  449.8432669   471.48663635  494.17133613
  517.94746792  542.86754393  568.9866029   596.36233166  625.05519253
  655.12855686  686.648845    719.685673    754.31200634  790.60432109
  828.64277285  868.51137375  910.29817799  954.09547635 1000.        ]

Let’s now specify the number of equally spaced values you want. For example, let’s generate 6 equal values on the log scale between 2 and 3.


# Six equally spaced values on log scale between 2 and 3
arr = np.logspace(2, 3, num=6)
print(arr)

# Output:
#[ 100.  158.48931925  251.18864315  398.10717055  630.95734448
# 1000.        ]

4. Get the Logscale of Array with out Endpoint

To get six values between 100 (base to the power 2, the start value) and 1000 (base to the power 3, the stop value). Use the False to the endpoint parameter, it won’t return the stop endpoint instead it will return before the start point.


# Exclude the stop endpoint
arr = np.logspace(2, 3, num=6, endpoint=False)
print(arr)

# Output:
# [100.  146.77992676 215.443469   316.22776602 464.15888336
# 681.29206906]

5. Get Log Scale by Using a Different Log Base

By default logspace() takes 10.0 as the base for the log scale. We can customize the base parameter and get the array of evenly spaced values. For example, let’s generate 7 equally spaced values on the log scale between 2 and 3 but use 2 as the base for the log scale this time.


# Use a different log base for the log scale
arr = np.logspace(2, 3, num=7, base=2)
print(arr)

# Output:
# [4.   4.48984819 5.0396842  5.65685425 6.34960421 7.12718975
#  8.  ]

6. Graphical Representation of numpy.logspace()

To get the graphical representation of numpy.logspace() use matplot library module. This module visualizes the even spaced values.


import numpy as np
import matplotlib.pyplot as plt

# Graphical representation of numpy.logspace() using matplotlib module
arr = 40
a1 = np.logspace(0.6, 4, arr, endpoint=True)
a2 = np.logspace(0.6, 4, arr, endpoint=False)
b = np.zeros(arr)
plt.plot(a1, b, 'o')
plt.ylim([-0.8, 4])
plt.show()

Yields below output.

NumPy logspace Python

You can convert these values with a log conversion and plot. For example, let’s plot the values returned from the np.logspace() function.


import numpy as np
import matplotlib.pyplot as plt
# Graphical representation of numpy.logspace() 
arr = np.logspace(2, 3, num=6)
arr1 = np.zeros(6)
plt.plot(arr, arr1, 'o')

Yields below output.

NumPy logspace Python

6. Conclusion

In this article, I have explained how to use the NumPy logspace() function and how to return numbers spaced evenly on a log scale with examples.

Happy Learning!!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.