我有一个功能:
f = x**0.5*numpy.exp(-x/150)
我使用 numpy 和 matplot.lib 生成 f 的图,作为 x 与 x 的函数:
x = np.linspace(0.0,1000.0, num=10.0)
我想知道如何创建一个随机 x 值数组将使用我首先制作的 x 数组为该函数创建相同的图?
布莱恩
我不太清楚你在问什么,但它只是想要你的“x”数组中的非规则间隔点那么简单吗?
如果是这样,请考虑对随机值数组进行累积求和。
举个简单的例子:
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()