[英]Use a more accurate array of x values to generate line of best fit in matplotlib?
I am currently stuck on a problem on which I am required to generate a curve of best fit which I am required to use a more precise x array from 250 to 100 in steps of 10. Here is my code below so far..我目前被困在一个问题上,我需要生成一条最佳拟合曲线,我需要以 10 为步长使用从 250 到 100 的更精确的 x 数组。到目前为止,这是我的代码。
import numpy as np
from numpy import polyfit, polyval
import matplotlib.pyplot as plt
x = [250,300,350,400,450,500,550,600,700,750,800,900,1000]
x = np.array(x)
y = [0.791, 0.846, 0.895, 0.939, 0.978, 1.014, 1.046, 1.075, 1.102, 1.148, 1.169, 1.204, 1.234]
y= np.array(y)
r = polyfit(x,y,3)
fit = polyval(r, x)
plt.plot(x, fit, 'b')
plt.plot(x,y, color = 'r', marker = 'x')
plt.show()
If I understand correctly, you are trying to create an array of numbers from a to b by steps of c.如果我理解正确,您正在尝试通过 c 的步骤创建从 a 到 b 的数字数组。
With pure python you can use:使用纯 python,您可以使用:
list(range(a, b, c)) #in your case list(range(250, 1000, 10))
Or, since you are using numpy you can directly make the numpy array:或者,由于您使用的是 numpy,您可以直接创建 numpy 数组:
np.arange(a, b, c)
To create an array in steps you can use numpy.arange([start,] stop[, step])
:要分步创建数组,您可以使用
numpy.arange([start,] stop[, step])
:
import numpy as np
x = np.arange(250,1000,10)
To generate values from 250-1000, use range(start, stop, step):要生成 250-1000 之间的值,请使用 range(start, stop, step):
x = range(250,1001,10)
x = np.array(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.