简体   繁体   English

如何使用 from scipy.interpolate import make_interp_spline 在 Python 中平滑此图

[英]How to smooth this Figure in Python with from scipy.interpolate import make_interp_spline

There is a plot which I want to make smooth for better representation.有一个 plot 我想让它变得平滑以便更好地表示。 I tried scipy.interpolate , however it produced this error:我试过scipy.interpolate ,但是它产生了这个错误:

raise ValueError("Expect x to be a 1-D sorted array_like.") ValueError: Expect x to be a 1-D sorted array_like. raise ValueError("Expect x to be a 1-D sorted array_like.") ValueError: Expect x to be a 1-D sorted array_like.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from scipy import interpolate
    from scipy.interpolate import make_interp_spline

    startnm = 550
    endnm = 700
    y = np.empty((10,))

    for aci in range(0, 91, 10):

    data = pd.read_csv(f".\\30mg-ml-PSQD-withZNO-12-nov-21\\{aci}.txt",
                       delimiter="\t") .to_numpy()[:, [0, 1]]
    print(type(data[0, 0]))
    starti, endi = 0, 0
    for i in range(len(data[:, 0])):
        if startnm < float(data[i, 0]) and starti == 0:
            starti = i
        elif endnm < float(data[i, 0]) and endi == 0:
            endi = i
            break

    y[aci//10] = np.sum(data[starti:endi, 1])


    theta = np.linspace(0, np.pi, 19)

    output = []
    x = []
    for i in range(10):
    temp0 = y[i]
    output.append(temp0*np.cos(theta[i])/y.max())
    x.append(temp0*np.sin(theta[i])/y.max())
    pass

    print(output)
    print(x)


    plt.title("title")
    plt.xlabel("x")
    plt.ylabel("y")

    plt.plot(x, output,"--")
    plt.plot(-np.array(x), output, "--")

    x = np.sin(theta)*np.cos(theta)
    y = np.cos(theta)*np.cos(theta)


    plt.plot(x, y, "r")
    plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)

 

    plt.show()

I want to smooth this graph as much as possible.我想尽可能地平滑这个图。 How can I do it?我该怎么做?

我想让虚线尽可能平滑

The error just tells you that the x array needs to be sorted.该错误只是告诉您需要对x数组进行排序。 Note also that make_interp_spline does not do any smoothing.还要注意make_interp_spline不做任何平滑。 For that, use splrep .为此,请使用splrep

My friend's solution to this problem:我朋友对这个问题的解决方法:

from scipy.interpolate import interp1d

f1 = interp1d(list(range(10)), x, kind="quadratic")
f2 = interp1d(list(range(10)), output, kind="quadratic")

xnew = f1(np.linspace(0, 8.9, 100))    
outnew = f2(np.linspace(0, 8.9, 100))




plt.plot(xnew, outnew)
plt.plot(-xnew, outnew, "b")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从scipy.interpolate导入interp1d挂django - from scipy.interpolate import interp1d hangs django scipy.interpolate.make_interp_spline 如何检索所有系数? - scipy.interpolate.make_interp_spline how to retrieve all the coefficients? 如何使用scipy.interpolate中的griddata - How to use griddata from scipy.interpolate 使用 scipy.interpolate 具有固定起点和终点的平滑样条近似 - Smooth spline approximation with fixed start and end points using scipy.interpolate Python:SciPy.interpolate分段多项式 - Python: SciPy.interpolate PiecewisePolynomial 线性插值和存储输出永远使用 python scipy.interpolate interp1d 和 for 循环 - linear interpolation and storing outputs taking forever using python scipy.interpolate interp1d and for loop scipy.interpolate 中的 interp1d 函数使用什么算法 - What algorithm used in interp1d function in scipy.interpolate scipy.interpolate中的三次样条函数返回numpy.ndarray - Cubic Spline function in scipy.interpolate returns a numpy.ndarray AttributeError: 模块 &#39;scipy.interpolate&#39; 没有属性 &#39;spline&#39; - AttributeError: module 'scipy.interpolate' has no attribute 'spline' 如何在 scipy.interpolate 中设置三次样条插值的第一个和最后一个斜率? - How to set first and last slope of cubic spline interpolation in scipy.interpolate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM