python 中的曲线拟合
嘿, 我有一组频率和功率谱的值,我必须在对数刻度上绘制功率谱与频率的关系。完成后,我需要通过它传递最适合的直线..我得到线性刻度上的线..但是当我尝试将其叠加到频率功率谱图上时,结果图不显示任何线,相反,第一个图的数据点只是在空间上移动。 此外,如果使用 loglog 函数在对数刻度上绘制相同的线,则不会显示。
有人可以告诉我应该做什么才能得到对数刻度上的线吗?
所以我有一个包含三列的文件;频率、功率规格电源信号..这是我写的一段用来绘制数据和线条的内容..
#initialize all variables to 0
#open the data file
while 1:
ln = datafile.readline()
if ln:
data = ln.split()
x = float(n)
y = float(data[0])
z = float(data[1])
xval.append(float(n))
yval.append(y)
zval.append(z)
n += 1
sum_z += z
sum_y += y
sum_y_squared += y*y
sum_yz += y*z
else:
break
datafile.close()
# calculate slope and intercept using formulae
for num in xval:
res = intercept + slope*num
line.append(res)
#Plot data
pylab.figure(0)
matplotlib.pylab.loglog(yval,zval)
#Plot line
pylab.figure(0)
pylab.plotloglog(line)
Hey,
I have a set of values for frequency and power spectrum and I have to plot Power spectrum Versus frequency on log scale. Once done, I need to pass the best fit straight line through it.. I get the line on a linear scale.. but when I try to superimpose it onto the freq-power spectrum plot, the resultant plot does not show any line, instead the data points of 1st plot are merely shifted in space.
Also, the same line, if plotted on log scale using loglog function, does not show up.
Can somebody tell me what I should do in order to get the line on a Log scale?
SO I have a file having three columns; Frequency, Power spec. Power signal.. Here is a piece of what i wrote to plot the data and line..
#initialize all variables to 0
#open the data file
while 1:
ln = datafile.readline()
if ln:
data = ln.split()
x = float(n)
y = float(data[0])
z = float(data[1])
xval.append(float(n))
yval.append(y)
zval.append(z)
n += 1
sum_z += z
sum_y += y
sum_y_squared += y*y
sum_yz += y*z
else:
break
datafile.close()
# calculate slope and intercept using formulae
for num in xval:
res = intercept + slope*num
line.append(res)
#Plot data
pylab.figure(0)
matplotlib.pylab.loglog(yval,zval)
#Plot line
pylab.figure(0)
pylab.plotloglog(line)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管您的示例中的绘图线命令不正确,但我认为它与您实际执行的操作类似。
第二个绘图命令在不同的 x 范围上绘图:
您还查看了 line 的值,它们是否有意义,它们与 yval、zval 的范围相同吗?
此外,您可能想使用
numpy.loadtxt
加载您的数据文件。Despite the fact that the plot line commands are not correct in your example I assume it is similar to what you actually do.
The second plot command plots on a different x range:
Also have you look at the values of line, do they make sense are they in the same range as yval, zval?
Additionally you might want to use
numpy.loadtxt
to load your data file.据我了解您的问题,您想在同一个图表中绘制两条线。一般是这样完成的:
所以,首先将它们放在同一个
Axes< /code>
,所以它们出现在同一个图中。要修改缩放比例,您可以使用
set_xscale
和set_yscale
分别。除此之外,我忍不住注意到你的读取文件的代码很糟糕。正如@Bernhard在他的回答中建议的那样,尝试使用
numpy.loadtxt
。这可能看起来像这样:这应该会给出与循环相同的结果,只是它更加简洁。我强烈建议您阅读 暂定 NumPy 教程,因为它解释了很多真正的知识numpy 数组的一些很酷的功能。
As I understand your problem, you want to plot two lines to the same diagram. Here is how it is done in general:
So, first you put them both in the same
Axes
, so they appear in the same diagram. TO modify the scaling, you can useset_xscale
andset_yscale
respectively.Apart from that, I cannot help but notice that your code for reading the file is horrible. As @Bernhard suggests in his answer, try using
numpy.loadtxt
. This could look like this:This should give you the same results as your loop, only it is much more concise. I strongly recommend you read the Tentative NumPy Tutorial, as it explain a lot of the really cool features of numpy arrays.