简体   繁体   English

平滑线 matplotlib:如何用 5 个样本点平滑线?

[英]smooth line matplotlib: How can i smooth line with 5 points of sample?

hope you are fine, i have the next question:希望你没事,我有下一个问题:

suppose i have 2 list with values x=mode, y=floor;假设我有 2 个列表,其值 x=mode, y=floor; my question is how can i join the points with smooth line using just 5 points:我的问题是如何仅使用 5 个点将点与平滑线连接起来:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
from scipy.optimize import curve_fit
from scipy import interpolate

mode=[0.0, -0.000906168, -0.000466564, 0.000320168, 0.000800825]
floor=[0, 3.2, 6.4, 9.6, 12.8]

plt.plot(mode1,floor,'green')
plt.plot(mode1,floor,'o')
plt.plot(np.repeat(0,len(floor)),floor,'o')
plt.plot(np.repeat(0,len(floor)),floor,'black')
plt.show()

the green plot is the line that i would like to have in smooth way, i was trying fitting with all libraries that you saw in the import but they don't bring me the good smooth, please i appreciate your helping.绿色图是我想要平滑的线条,我试图适应您在导入中看到的所有库,但它们并没有给我带来良好的平滑效果,请我感谢您的帮助。

Scipy's interpolate.interp1d with either kind='cubic' or kind='quadratic' can be used to create a smooth curve. Scipy 的带有kind='cubic'kind='quadratic'interpolate.interp1d可用于创建平滑曲线。 A quadratic curve looks a bit stiffer than a cubic.二次曲线看起来比三次曲线硬一点。 Note that in the example x and y are reversed to how they're usually drawn.请注意,在示例中 x 和 y 与它们通常的绘制方式相反。

Here is some example code to demonstrate how such an interpolation can be used in your situation:下面是一些示例代码,用于演示如何在您的情况下使用这种插值:

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

mode = [0.0, -0.000906168, -0.000466564, 0.000320168, 0.000800825]
floor = [0, 3.2, 6.4, 9.6, 12.8]

plt.plot(mode, floor, marker='o', markerfacecolor='dodgerblue', markeredgecolor='dodgerblue',
         linestyle='dotted', color='limegreen', linewidth=1)
plt.plot(np.zeros(len(floor)), floor, marker='o', markerfacecolor='orange', markeredgecolor='orange',
         linestyle='solid', color='purple')

smoothed_mode = interpolate.interp1d(floor, mode, 'cubic')
floor_range = np.linspace(min(floor), max(floor), 500)
plt.plot(smoothed_mode(floor_range), floor_range, ls='solid', color='crimson')

plt.show()

结果图

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM