【发布时间】:2020-04-22 07:39:44
【问题描述】:
我有一条曲线,我想固定起点并将终点拖动到蓝点,同时保持曲线的一般形状(拉伸和收缩)。有什么想法我该怎么做?
import matplotlib.pyplot as pl
import numpy as np
x = np.linspace(1, 10)
def f(x):
return np.sin(x) + np.random.normal(scale=0.1, size=len(x))
pl.plot(x, f(x))
pl.plot(8,.2,'bo')
回答
#shift the end point to the dot
shift_x = 10-x[-1]
shift_y = .2-y[-1]
#shift the data
x=x+shift_x
y=y+shift_y
pl.plot(x,y)
pl.plot(10,.2,'bo')
shift end point to desired location
#shift the data back to the original points based on how close it is
#to the end point
gradient_x = shift_x/len(x)
gradient_y = shift_y/len(y)
for i in range(len(x)):
y[i] = y[i]-(len(x)-i)*gradient_y
x[i] = x[i]-(len(x)-i)*gradient_x
pl.plot(x,y)
pl.plot(10,.2,'bo')
【问题讨论】:
-
到目前为止您尝试了哪些方法,您遇到了哪些问题?也许测量
x,y从当前y到所需x,y(点)的距离并相应地延长x?您能否介绍一下您项目的背景? -
好吧,我需要像弹簧一样挤压曲线,我觉得应该有一个简单的算法来做到这一点,但我只是卡住了。