伸展曲线到达点python

问题描述 投票:0回答:1

我有一条曲线,我想固定起点并将终点拖到蓝点,同时保持曲线的总体形状(拉伸和收缩)。有什么想法我该怎么做?

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')

Curve with Point

答案


#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')

All 3 Tranformations

python line stretch
1个回答
0
投票

您可以使用简单的比例尺拉伸:

x_scale = (blue_x - x1) / (x1 - x0)
y_scale = (blue_y - y1) / (y1 - y0)

然后转换曲线中的所有点:

new_x = (old_x - x0) * x_scale + x0
new_y = (old_y - y0) * y_scale + y0

假设(x0,y0)是曲线的起点。

© www.soinside.com 2019 - 2024. All rights reserved.