简体   繁体   中英

Python curve for a lot points

I'm using Python and 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). 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. So i don't wann connect the points i wanna have a fit. So yeah I say fit to this :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). a seven degree polynomial is certainly overfitting this small amount of data, and these look like a reasonable fits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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