- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有两条曲线,如下所示:
我正在寻找一种方法,通过将前者(蓝线)向上和向右延伸来平滑连接蓝色曲线和红色曲线,同时离开后者(红线)未受影响。方向很重要,我提到这一点是因为看起来继续向左走蓝线会更容易。我不能这样做(这在我的较大代码中没有意义)所以它必须向上和向右。
这是我到目前为止所做的(放大了两条线靠近的部分):
基本上,我使用来自两条曲线(黑点)的点样本插入一条新曲线。获取此图的 MWE
代码如下。
我现在需要做的是找到一种方法,将绿线从与红线相交的点修剪到与蓝线相交的点,并延长蓝线替换现在不再需要最后一段。
这是应用上述更改后蓝线的样子(手工制作):
绿线的修剪部分现在是蓝线的一部分。请注意,我有:
因为我已经有了插值曲线(绿线),所以我只需要一种方法:
在这个特定的例子中,我使用了固定列表来绘制和执行计算,但是我有几对线需要执行类似的操作,所以解决方案必须足够通用才能考虑以下情况形状相似但曲线不同。我怎么能这样做?
我愿意接受使用 numpy
、scipy
或任何必要的解决方案。
这是MWE
:
import numpy as np
import matplotlib.pyplot as plt
# Red line data.
x1 = [0.01, 0.04, 0.08, 0.11, 0.15, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35, 0.38, 0.41, 0.44, 0.46, 0.49, 0.51, 0.54, 0.56, 0.58]
y1 = [2.04, 2.14, 2.24, 2.34, 2.44, 2.54, 2.64, 2.74, 2.84, 2.94, 3.04, 3.14, 3.24, 3.34, 3.44, 3.54, 3.64, 3.74, 3.84, 3.94]
# Blue line data.
x2 = [0.4634, 0.4497, 0.4375, 0.4268, 0.4175, 0.4095, 0.4027, 0.3971, 0.3925, 0.389, 0.3865, 0.3848, 0.384, 0.3839, 0.3845, 0.3857, 0.3874, 0.3896, 0.3922, 0.3951, 0.3982, 0.4016, 0.405, 0.4085, 0.412, 0.4154, 0.4186, 0.4215, 0.4242, 0.4265, 0.4283, 0.4297, 0.4304, 0.4305, 0.4298, 0.4284, 0.4261, 0.4228, 0.4185, 0.4132, 0.4067, 0.399, 0.39, 0.3796, 0.3679, 0.3546, 0.3397, 0.3232, 0.305, 0.285]
y2 = [1.0252, 1.0593, 1.0934, 1.1275, 1.1616, 1.1957, 1.2298, 1.2639, 1.298, 1.3321, 1.3662, 1.4003, 1.4344, 1.4685, 1.5026, 1.5367, 1.5708, 1.6049, 1.639, 1.6731, 1.7072, 1.7413, 1.7754, 1.8095, 1.8436, 1.8776, 1.9117, 1.9458, 1.9799, 2.014, 2.0481, 2.0822, 2.1163, 2.1504, 2.1845, 2.2186, 2.2527, 2.2868, 2.3209, 2.355, 2.3891, 2.4232, 2.4573, 2.4914, 2.5255, 2.5596, 2.5937, 2.6278, 2.6619, 2.696]
x3, y3 = [], []
# Store a small section of the blue line in these new lists: only those points
# closer than 0.2 to the last point in this line.
for indx,y2_i in enumerate(y2):
if (y2[-1]-y2_i)<=0.2:
y3.append(y2_i)
x3.append(x2[indx])
# The same as above but for the red line: store only those points between
# 0. and 0.4 in the y axis and with a larger x value than the last point in the
# blue line.
for indx,y1_i in enumerate(y1):
if 0. <(y1_i-y2[-1])<=0.4 and x1[indx] > x2[-1]:
y3.append(y1_i)
x3.append(x1[indx])
# Find interpolating curve that joins both segments stored in x3,y3.
poli_order = 3 # Order of the polynome.
poli = np.polyfit(y3, x3, poli_order)
y_pol = np.linspace(min(y3), max(y3), 50)
p = np.poly1d(poli)
x_pol = [p(i) for i in y_pol]
plt.plot(x1,y1, 'r')
plt.plot(x2,y2, 'b')
plt.plot(x_pol,y_pol, 'g')
plt.scatter(x3,y3,c='k')
plt.show()
最佳答案
正如其他人提到的,尝试使用样条曲线。您的平滑曲线在导数中不是那么平滑,从连续线到直线看起来像 f'(x) 中的不连续点。因此,我将边界从 0.4 收紧到 0.2,这只捕获了红线拟合的一个点。否则,样条曲线将围绕额外的红点进行过度插值。
使用 defs 的简单示例。从上面:
from scipy.interpolate import spline
sx = np.array(x2+x3)
sy = np.array(y2+y3)
t = np.arange(sx.size,dtype=float)
t /= t[-1]
N = np.linspace(0,1,2000)
SX = spline(t,sx,N,order=4)
SY = spline(t,sy,N,order=4)
plt.plot(x1,y1, 'r')
plt.plot(x2,y2, 'b')
plt.scatter(x3,y3,c='k')
plt.plot(SX, SY,'g',alpha=.7,lw=3)
plt.show()
这个问题是一个方便的引用:
Smooth spline representation of an arbitrary contour, f(length) --> x,y
关于python - 延长线与另一条线平滑连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643637/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!