Home > database >  python logspace curve generation
python logspace curve generation

Time:06-27

I would like to generate a logarithmic curve between two values, with a set number of datapoints in between.

I tried np.logspace(start,end,num_points) but that has an issue:
The created values are much higher than I expected (I believe this is due to the base value?)

How can I create values to be in the input range (with the distance between them being logarithmic)? i.e. start=10; end=12; num_points=6
output: array([10, 11, 11.5, 11.75, 11,9, 12]) (the numbers in the output are just my estimation, real values will be different).

My end goal is to plot these values (y-axis) against the num_points (as x-axis). plt.plot(num_point,output)

CodePudding user response:

You can log a linspace with start and end points raised correspondingly:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(range(6), [10, 11, 11.5, 11.75, 11.9, 12], 'ko-', label='requested')
plt.plot(range(6), np.log10(np.linspace(10**10, 10**12, 6)), 'ro-', label='log10')
plt.plot(range(6), np.log(np.linspace(np.exp(10), np.exp(12), 6)), 'bo-', label='log')
plt.legend()

enter image description here

CodePudding user response:

I believe you are looking for geomspace, e.g.

import numpy as np
print(np.geomspace(10, 12, 6))
#[10.         10.37137289 10.75653757 11.15600622 11.57031005 #12.        ]
  • Related