【问题标题】:Using Pylab to create a plot of a line and then getting the rasterized data from the line使用 Pylab 创建一条线图,然后从该线获取栅格化数据
【发布时间】:2011-12-12 01:33:15
【问题描述】:

我正在尝试从 pylab 绘图函数中获取栅格化线数据。我的代码是这样的:

fitfunc = lambda p, x: p[0] + p[1] * sin(2 * pi * x / data[head[0]].size + p[2])
errfunc = lambda p, x, y: fitfunc(p, x) - y

data = np.genfromtxt(dataFileName, dtype=None, delimiter='\t', names=True)

xAxisSeries =linspace(0., data[head[0]].max(), data[head[0]].size)

p0 = [489., 1000., 9000.] # Initial guess for the parameters
p1, success = optimize.leastsq(errfunc, p0[:], args=(xAxisSeries, data[head[1]]))

time = linspace(xAxisSeries.min(), xAxisSeries.max(), 1000)

plotinfo = plot(time, fitfunc(p1, time), 'r-')

我想从 plotinfo 获取 x 和 y 线数据。当我使用“type(plotinfo)”时,plotinfo 是一个列表,但是当我使用“print plotinfo”时,它是一个 2dlist 对象。

【问题讨论】:

  • (time,fitfunc(p1,time)) 不是您要查找的(x,y) 数据吗?或者你想要它在 pylab 图形的像素坐标中吗?还是别的什么?
  • 如果我理解正确,你想要插值数据吗?
  • 你可以从你的函数中得到你想要的每一个点,为什么要使用插值?如果你想插值使用:np.interp(new_x, old_x, old_y)

标签: python matlab numpy scipy matplotlib


【解决方案1】:
import numpy as np
import matplotlib.pyplot as plt
N=4
x=np.linspace(0, 10, N)
y=np.cumsum(np.random.random(N) - 0.5)
line=plt.plot(x,y)[0]
path=line._path

这些是原始 (x,y) 数据点:

print(path.vertices)
# [[  0.           0.08426592]
#  [  3.33333333   0.14204252]
#  [  6.66666667   0.41860647]
#  [ 10.           0.22516175]]

在这里,我们(线性)插值以找到额外的点。您可以将参数增加到path.interpolated 以在原始点之间找到更多的插值点。

path2=path.interpolated(2)
print(path2.vertices)
# [[  0.           0.08426592]
#  [  1.66666667   0.11315422]
#  [  3.33333333   0.14204252]
#  [  5.           0.2803245 ]
#  [  6.66666667   0.41860647]
#  [  8.33333333   0.32188411]
#  [ 10.           0.22516175]]

【讨论】:

  • 有没有办法获取 0,1,2,3...到 10 的值?没关系,我只是重新阅读了您的帖子。非常感谢!
  • 如果你想要特定点的插值,我会使用tillsten的想法:y2=np.interp(range(11),x,y)。使用它,不需要matplotlib。
  • 他可以使用fitfunc(p1, range(11))。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多