简体   繁体   English

Python曲线有很多要点

[英]Python curve for a lot points

I'm using Python and matplotlib. 我正在使用Python和matplotlib。 I have a lot of Points, generated with arrays. 我有很多由数组生成的积分。 在此处输入图片说明

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=Groesse_cm/2.54)
ax.set_title(title)
ax.set_xlabel(xlabel) # Beschriftung X-Achse
ax.set_ylabel(ylabel) # Beschriftung Y-Achse
ax.plot(xWerte, yWerte, 'ro', label=kurveName)
ax.plot(xWerte, y2Werte, 'bo', label=kurveName2)
plt.show()

So I have the arrayX for x Values and the arrayYmax for Y Values (red) and arrayYmin for Y Values (blue). 所以我有arrayX的x值和arrayYmax的Y值(红色)和arrayYmin的Y值(蓝色)。 I can't give you my arrays, couse that is much too complicated. 我不能给你我的数组,因为那太复杂了。

My question is: How can I get a spline/fit like in the upper picture? 我的问题是:如何获得上图所示的样条线/拟合? I do not know the function of my fited points, so I have just Points with [x / y] Values. 我不知道拟合点的功能,因此我只有带有[x / y]值的点。 So i don't wann connect the points i wanna have a fit. 所以我不想把我想适合的要点联系起来。 So yeah I say fit to this :D 是的,我说适合这个:D

Here is an example i don't wanna have: The code for this is: 这是我不想要的示例:此代码为:

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=Groesse_cm/2.54)
degree = 7
np.poly1d(np.polyfit(arrayX,arrayYmax,degree))
ax.plot(arrayX, arrayYmax, 'r')
np.poly1d(np.polyfit(arrayX,arrayYmin,degree))
ax.plot(arrayX, arrayYmin, 'b')
#Punkte
ax.plot(arrayX, arrayYmin, 'bo')
ax.plot(arrayX, arrayYmax, 'ro')
plt.show()

在此处输入图片说明

you're pretty close, you just need to use the polynomial model you're estimating/fitting. 您已经很接近了,您只需要使用要估算/拟合的多项式模型即可。

start with pulling in packages and defining your data: 首先提取软件包并定义数据:

import numpy as np
import matplotlib.pyplot as plt

arr_x = [-0.8,  2.2,  5.2,  8.2, 11.2, 14.2, 17.2]
arr_y_min = [65, 165, 198, 183, 202, 175, 97]
arr_y_max = [618, 620, 545, 626, 557, 626, 555]

then we estimate the polynomial fit, as you were doing, but saving the result into a variable that we can use later: 然后我们像您所做的那样估算多项式拟合,但是将结果保存到一个变量中,以备后用:

poly_min = np.poly1d(np.polyfit(arr_x, arr_y_min, 2))
poly_max = np.poly1d(np.polyfit(arr_x, arr_y_max, 1))

next we plot the data: 接下来我们绘制数据:

plt.plot(arr_x, arr_y_min, 'bo:')
plt.plot(arr_x, arr_y_max, 'ro:')

next we use the polynomial fit from above to plot estimated value at a set of sampled points: 接下来,我们从上方使用多项式拟合在一组采样点上绘制估计值:

poly_x = np.linspace(-1, 18, 101)

plt.plot(poly_x, poly_min(poly_x), 'b')
plt.plot(poly_x, poly_max(poly_x), 'r')

giving us: 给我们:

多项式拟合

note that I'm using much lower degree polynomials (1 and 2) than you (7). 请注意,我使用的次数多项式(1和2)比您(7)低得多。 a seven degree polynomial is certainly overfitting this small amount of data, and these look like a reasonable fits 一个7度多项式肯定会过分拟合这少量数据,这些看起来像是合理的拟合

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

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