Как сгладить линию в python3? - PullRequest
       7

Как сгладить линию в python3?

1 голос
/ 19 февраля 2020

Как сгладить эту синюю линию, пожалуйста?

import numpy as np
from matplotlib import pyplot as plt

a = [0.5365140382771445, 0.5214107372135204, 0.49844631119258076, 0.4681910992517213, 0.4310817214420628, 0.3882500155177606, 0.340292343154754, 0.2880252732908801]
b = [0.7416012836460293, 0.697385422521102, 0.6561831711375956, 0.6187959941327967, 0.585900754784896, 0.5586375446776617, 0.537388969490203, 0.5229339200070606]

time_fit = np.arange(len(b))
fitx = np.polyfit(time_fit, a, 2)
fity = np.polyfit(time_fit, b, 2)
x_fit = np.poly1d(fitx)
y_fit = np.poly1d(fity)

plt.figure(figsize=(8, 8))
plt.plot(x_fit, y_fit, c='red')
plt.plot(a, b, c = 'blue')
plt.show()

Подходит ли мне неправильный полином?

enter image description here

1 Ответ

1 голос
/ 19 февраля 2020

Итак, вы пытаетесь подогнать полиномиальную кривую к вашим точкам. Давайте разберемся, как работает подгонка кривой для 2d точек. Для простоты я выбрал полином 2-го порядка, чтобы соответствовать точкам. Кроме того, я позволил себе переименовать имена ваших точек (a -> x и b -> y).

np.polyfit возвращает полиномиальные коэффициенты в соответствии с указанным вами заказом. Вы можете создать фактический полиномиальный класс, применив np.poly1d к возвращенным коэффициентам. Чтобы построить полином, мы подходим к x и получаем z. Эти (x,z) точки - это наша подогнанная кривая.

import numpy as np
from matplotlib import pyplot as plt


x = [
    0.5365140382771445,
    0.5214107372135204,
    0.49844631119258076,
    0.4681910992517213,
    0.4310817214420628,
    0.3882500155177606,
    0.340292343154754,
    0.2880252732908801,
]
y = [
    0.7416012836460293,
    0.697385422521102,
    0.6561831711375956,
    0.6187959941327967,
    0.585900754784896,
    0.5586375446776617,
    0.537388969490203,
    0.5229339200070606,
]

# fitting x, y in a second order equation
# this returns the coefficient of p_0*x^2 + p_1*x + p_n = 0
# building the polynomial
poly = np.poly1d(np.polyfit(x, y, 2))

# getting the points for plotting polynomial
z = poly(x)

plt.figure(figsize=(8, 8))

# scatter plotting the actual value
plt.scatter(x, y, c="blue", marker="o", label="original points")

# plotting the polynomial curve
plt.plot(x, z, c="red", label="fitted curve (2nd order)")
plt.legend()
plt.show()

Это возвращает

enter image description here

...