pythonnumpy

Produce evenly spaced values on a logarithmic scale (like `np.linspace()`, but on a log scale)


I want to create two arrays, first a y array with values in a range from 10^3 to 10^10 and another x array with values in a range from 10^-5 to 10^10 for a logarithmic plot. I tried the following :

y = np.linspace(1e3,1e10, num = 1000)
x = np.linspace(1e-5,1e10, num = 1000) 

But it returns me a non evenly distributed sample, with only 1 value of the order of 10^-5 and many more of the order of 10^9 for x, and a zero value between 10^-5 and 10^7. That is what I get for x:

 [1.00000000e-05 1.00100100e+07 2.00200200e+07 3.00300300e+07
 4.00400400e+07 5.00500501e+07 6.00600601e+07 7.00700701e+07
 8.00800801e+07 9.00900901e+07 1.00100100e+08 1.10110110e+08
 1.20120120e+08 1.30130130e+08 1.40140140e+08 1.50150150e+08
 ...

I want an array with values evenly separated: with the same number of values for each 10^ order because I need it for a logarithmic plot. Why is linspace not working, and how can I fix it?


Solution

  • You are using the wrong function for what you are trying to achieve here. You should use "logspace" for that.

    y = np.logspace(3,10, num = 1000)
    print (y)