Reputation: 1191
I have a function:
f = x**0.5*numpy.exp(-x/150)
I used numpy and matplot.lib to generate a plot of f as a function of x with x:
x = np.linspace(0.0,1000.0, num=10.0)
I am wondering how do I create an array of random x values would create the same plot for this function using the x array I first made?
Bryan
Upvotes: 4
Views: 2751
Reputation: 284870
I'm not quite clear what you're asking, but is it as simple as just wanting non-regularly-spaced points in your "x" array?
If so consider doing a cumulative summation on an array of random values.
As a quick example:
import numpy as np
import matplotlib.pyplot as plt
xmin, xmax, num = 0, 1000, 20
func = lambda x: np.sqrt(x) * np.exp(-x / 150)
# Generate evenly spaced data...
x_even = np.linspace(xmin, xmax, num)
# Generate randomly spaced data...
x = np.random.random(num).cumsum()
# Rescale to desired range
x = (x - x.min()) / x.ptp()
x = (xmax - xmin) * x + xmin
# Plot the results
fig, axes = plt.subplots(nrows=2, sharex=True)
for x, ax in zip([x_even, x_rand], axes):
ax.plot(x, func(x), marker='o', mfc='red')
axes[0].set_title('Evenly Spaced Points')
axes[1].set_title('Randomly Spaced Points')
plt.show()
Upvotes: 5