python 中的曲线拟合

发布于 2024-10-20 05:25:28 字数 878 浏览 7 评论 0原文

嘿, 我有一组频率和功率谱的值,我必须在对数刻度上绘制功率谱与频率的关系。完成后,我需要通过它传递最适合的直线..我得到线性刻度上的线..但是当我尝试将其叠加到频率功率谱图上时,结果图不显示任何线,相反,第一个图的数据点只是在空间上移动。 此外,如果使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-10-27 05:25:28

尽管您的示例中的绘图线命令不正确,但我认为它与您实际执行的操作类似。

第二个绘图命令在不同的 x 范围上绘图:

loglog(yval,zval) # plot  yval vs zval
loglog(line) #  plots range(0,len(line)) vs line

您还查看了 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:

loglog(yval,zval) # plot  yval vs zval
loglog(line) #  plots range(0,len(line)) vs line

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.

晨光如昨 2024-10-27 05:25:28

据我了解您的问题,您想在同一个图表中绘制两条线。一般是这样完成的:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(line1_x, line1_y)
ax.plot(line2_x, line2_y)
ax.set_yscale("log")

所以,首先将它们放在同一个 Axes< /code>,所以它们出现在同一个图中。要修改缩放比例,您可以使用 set_xscale set_yscale 分别。

除此之外,我忍不住注意到你的读取文件的代码很糟糕。正如@Bernhard在他的回答中建议的那样,尝试使用 numpy.loadtxt。这可能看起来像这样:

data = numpy.loadtxt("data.txt")
n = len(data)
x = numpy.arange(n)
sum_z = sum(data.T[1])
sum_y = sum(data.T[0])
sum_y_squared = sum(data.T[0]**2)
sum_yz = sum(data.T[0]*data.T[1])

这应该会给出与循环相同的结果,只是它更加简洁。我强烈建议您阅读 暂定 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:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(line1_x, line1_y)
ax.plot(line2_x, line2_y)
ax.set_yscale("log")

So, first you put them both in the same Axes, so they appear in the same diagram. TO modify the scaling, you can use set_xscale and set_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:

data = numpy.loadtxt("data.txt")
n = len(data)
x = numpy.arange(n)
sum_z = sum(data.T[1])
sum_y = sum(data.T[0])
sum_y_squared = sum(data.T[0]**2)
sum_yz = sum(data.T[0]*data.T[1])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文