用由少量元素组成的向量平滑曲线?

2024-04-21

我有 4 条曲线,由这些向量表示:

x = [300, 700, 1000, 1500]
y1 = [-1.0055394199673442, -0.11221578805214968, -1.502661406039569, 1.0216939169819494]
y2 = [-1.0200777228890747, -0.6951505674297687, -2.832988761335546, 1.0253075071285915]
y3 = [2.0502387421569463, -1.3363305947335058, 0.2893545237634795, 0.8692051683379767]
y4 = [1.8676528391899183, -1.7554177636905024, 0.2364994810496486, 0.9811885784744991]

当我绘制它们时,我得到这样的东西:

正如您所看到的,这些向量的少数值导致这些曲线呈锯齿形,我希望它们更加平滑。我在 Python 和 Matlab 中尝试了不同的方法。例如,在 python 中,我使用了 numpy 方法,例如:

xp = np.linspace(300,1500,100,endpoint=True)
z1 = np.polyfit(x,  y1, 3)
p1 = np.poly1d(z1)
z2 = np.polyfit(x, y2, 3)
p2 = np.poly1d(z2)
z3 = np.polyfit(x,y3, 3)
p3 = np.poly1d(z3)
z4 = np.polyfit(x, y4, 3)
p4 = np.poly1d(z4)

我得到了这个:

但通过这种方式,我不确定 x 向量之间的值是否存在最小值和最大值点。 另外,我尝试了Matlab中具有不同平滑值的“平滑”函数,但它们也改变了向量内的值,这些向量不再像原始向量,即它们对于我想解释的概念毫无意义。 我想稍微平滑一下绘图的之字形部分,使曲线看起来更舒服......你能帮我吗? Python 或 Matlab 对我来说都是一样的,我对一切都持开放态度! :)


好吧,所以我在这里建议的是作弊和发明数据,但至少它使曲线看起来更像你(或你的主管)想要的。

x =  [300,      700,      1000,       1500] # your original x
x2 = [300, 500, 700, 850, 1000, 1250, 1500] # add points in between

# interpolate your data for the new points in x2
p1 = np.interp(x2,x,y1)
p2 = np.interp(x2,x,y2)
p3 = np.interp(x2,x,y3)
p4 = np.interp(x2,x,y4)

# cubic spline interpolation on xp, so it looks smooth
p1 = scipy.interpolate.CubicSpline(x2,p1)
p2 = scipy.interpolate.CubicSpline(x2,p2)
p3 = scipy.interpolate.CubicSpline(x2,p3)
p4 = scipy.interpolate.CubicSpline(x2,p4)

它看起来是这样的:

如果您对此外观不满意,可以尝试不同的值x2.

EDIT:

这是生成该图的完整代码:

import numpy as np
from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt

x =  [300,      700,      1000,       1500] # your orginial x
x2 = [300, 500, 700, 850, 1000, 1250, 1500] # add points in between
xp = np.linspace(300,1500,100,endpoint=True) # your x-axis for smooth curve plot

# your orginal data
y1 = [-1.0055394199673442, -0.11221578805214968, -1.502661406039569, 1.0216939169819494]
y2 = [-1.0200777228890747, -0.6951505674297687, -2.832988761335546, 1.0253075071285915]
y3 = [2.0502387421569463, -1.3363305947335058, 0.2893545237634795, 0.8692051683379767]
y4 = [1.8676528391899183, -1.7554177636905024, 0.2364994810496486, 0.9811885784744991]

for yi in [y1,y2,y3,y4]:
    # Piecewise linear interpolation of data y over the points x2
    y_interpolated_over_x2 = np.interp(x2,x,yi)

    # Make a cubic spline from the manipulated data
    y_cubic_spline = CubicSpline(x2, y_interpolated_over_x2)

    # The smooth curve is the cubic spline evaluated at points xp
    y_smooth = y_cubic_spline(xp)

    plt.plot(xp, y_smooth) # plot the smooth curve
    plt.scatter(x, yi) # plot the original data points

plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用由少量元素组成的向量平滑曲线? 的相关文章

  • 使用 openCV 对图像中的子图像进行通用检测

    免责声明 我是计算机视觉菜鸟 我看过很多关于如何在较大图像中查找特定子图像的堆栈溢出帖子 我的用例有点不同 因为我不希望它是具体的 而且我不确定如何做到这一点 如果可能的话 但我感觉应该如此 我有大量图像数据集 有时 其中一些图像是数据集的
  • 如何使用固定的 pandas 数据框进行动态 matplotlib 绘图?

    我有一个名为的数据框benchmark returns and strategy returns 两者具有相同的时间跨度 我想找到一种方法以漂亮的动画风格绘制数据点 以便它显示逐渐加载的所有点 我知道有一个matplotlib animat
  • DreamPie 不适用于 Python 3.2

    我最喜欢的 Python shell 是DreamPie http dreampie sourceforge net 我想将它与 Python 3 2 一起使用 我使用了 添加解释器 DreamPie 应用程序并添加了 Python 3 2
  • Python 多处理示例不起作用

    我正在尝试学习如何使用multiprocessing但我无法让它发挥作用 这是代码文档 http docs python org 2 library multiprocessing html from multiprocessing imp
  • 如何等到 Excel 计算公式后再继续 win32com

    我有一个 win32com Python 脚本 它将多个 Excel 文件合并到电子表格中并将其另存为 PDF 现在的工作原理是输出几乎都是 NAME 因为文件是在计算 Excel 文件内容之前输出的 这可能需要一分钟 如何强制工作簿计算值
  • 如何在 MATLAB 中绘制 3D 曲面图?

    我有一个像这样的数据集 0 1 0 2 0 3 0 4 1 10 11 12 13 2 11 12 13 14 3 12 13 14 15 4 13 14 15 16 我想在 matlab 中绘制 3D 曲面图 使列标题位于 y 轴 行标题
  • 如何使用 Scrapy 从网站获取所有纯文本?

    我希望在 HTML 呈现后 可以从网站上看到所有文本 我正在使用 Scrapy 框架使用 Python 工作 和xpath body text 我能够获取它 但是带有 HTML 标签 而且我只想要文本 有什么解决办法吗 最简单的选择是ext
  • 为 pandas 数据透视表中的每个值列定义 aggfunc

    试图生成具有多个 值 列的数据透视表 我知道我可以使用 aggfunc 按照我想要的方式聚合值 但是如果我不想对两列求和或求平均值 而是想要一列的总和 同时求另一列的平均值 该怎么办 那么使用 pandas 可以做到这一点吗 df pd D
  • Python tcl 未正确安装

    我刚刚为 python 安装了graphics py 但是当我尝试运行以下代码时 from graphics import def main win GraphWin My Circle 100 100 c Circle Point 50
  • 从列表中的数据框列中搜索部分字符串匹配 - Pandas - Python

    我有一个清单 things A1 B2 C3 我有一个 pandas 数据框 其中有一列包含用分号分隔的值 某些行将包含与上面列表中的一项的匹配 它不会是完美的匹配 因为它在其中包含字符串的其他部分 该列 例如 该列中的一行可能有 哇 这里
  • python pandas 中的双端队列

    我正在使用Python的deque 实现一个简单的循环缓冲区 from collections import deque import numpy as np test sequence np array range 100 2 resha
  • Python:字符串不会转换为浮点数[重复]

    这个问题在这里已经有答案了 我几个小时前写了这个程序 while True print What would you like me to double line raw input gt if line done break else f
  • Pandas Dataframe 中 bool 值的条件前向填充

    问题 如何转发 fill boolTruepandas 数据框中的值 如果是当天的第一个条目 True 到一天结束时 请参阅以下示例和所需的输出 Data import pandas as pd import numpy as np df
  • 如何将 numpy.matrix 提高到非整数幂?

    The 运算符为numpy matrix不支持非整数幂 gt gt gt m matrix 1 0 0 5 0 5 gt gt gt m 2 5 TypeError exponent must be an integer 我想要的是 oct
  • Numpy 优化

    我有一个根据条件分配值的函数 我的数据集大小通常在 30 50k 范围内 我不确定这是否是使用 numpy 的正确方法 但是当数字超过 5k 时 它会变得非常慢 有没有更好的方法让它更快 import numpy as np N 5000
  • 从 pygame 获取 numpy 数组

    我想通过 python 访问我的网络摄像头 不幸的是 由于网络摄像头的原因 openCV 无法工作 Pygame camera 使用以下代码就像魅力一样 from pygame import camera display camera in
  • 从 Python 中的类元信息对 __init__ 函数进行类型提示

    我想做的是复制什么SQLAlchemy确实 以其DeclarativeMeta班级 有了这段代码 from sqlalchemy import Column Integer String from sqlalchemy ext declar
  • 协方差矩阵的对角元素不是 1 pandas/numpy

    我有以下数据框 A B 0 1 5 1 2 6 2 3 7 3 4 8 我想计算协方差 a df iloc 0 values b df iloc 1 values 使用 numpy 作为 cov numpy cov a b I get ar
  • Python:元类属性有时会覆盖类属性?

    下面代码的结果让我感到困惑 class MyClass type property def a self return 1 class MyObject object metaclass MyClass a 2 print MyObject
  • PyAudio ErrNo 输入溢出 -9981

    我遇到了与用户相同的错误 Python 使用 Pyaudio 以 16000Hz 录制音频时出错 https stackoverflow com questions 12994981 python error audio recording

随机推荐