【问题标题】:How to smooth this Figure in Python with from scipy.interpolate import make_interp_spline如何使用 from scipy.interpolate import make_interp_spline 在 Python 中平滑此图形
【发布时间】:2022-01-10 11:03:07
【问题描述】:

为了更好地表现,我想让一个情节变得平滑。我试过scipy.interpolate,但是它产生了这个错误:

raise ValueError("期望 x 是一维排序的 array_like。") ValueError: 期望 x 是一维排序的 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()

我想尽可能地平滑这个图表。我该怎么做?

【问题讨论】:

    标签: python scipy smoothing


    【解决方案1】:

    错误只是告诉您x 数组需要排序。 另请注意,make_interp_spline 不会进行任何平滑处理。为此,请使用 splrep 。

    【讨论】:

      【解决方案2】:

      我朋友对这个问题的解决方法:

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

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
      猜你喜欢
      • 2021-03-07
      • 1970-01-01
      • 2019-11-16
      • 2021-01-31
      • 2021-11-28
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多