Python 中数据集的样条线与曲线

问题描述 投票:0回答:1

我目前正在开发一个项目,正在绘制图表。我拥有所有数据,并且确信方程已正确应用,但是我的图表与预期的绘图不符。

有没有办法调整图表上的曲线使其更加稳定,直到达到 x 轴上的某个位置?

This is the desired plot

This is my plot

我尝试过使用 scipy.curvefit() 和 polyfit() 但是 curvefit 不会绘制任何图形,并且 polyfit 不允许我在调用它时使用大于 1 的多项式。我已经阅读了有关 polyfit 的文档,似乎无法弄清楚为什么我不能使用 polyfit(t,Ub,3)。我正在考虑的另一个选择是样条曲线,但不确定这是否会产生所需的结果。 想知道这里是否有人能够提供帮助?

这是我正在使用的代码:


``import matplotlib.pyplot as plt
t = [  0.        ,   0.6012024 ,   1.20240481,   1.80360721,
     2.40480962,   3.00601202,   3.60721443,   4.20841683,
     4.80961924,   5.41082164,   6.01202405,   6.61322645,
     7.21442886,   7.81563126,   8.41683367,   9.01803607,
     9.61923848,  10.22044088,  10.82164329,  11.42284569,
    12.0240481 ,  12.6252505 ,  13.22645291,  13.82765531,
    14.42885772,  15.03006012,  15.63126253,  16.23246493,
    16.83366733,  17.43486974,  18.03607214,  18.63727455,
    19.23847695,  19.83967936,  20.44088176,  21.04208417,
    21.64328657,  22.24448898,  22.84569138,  23.44689379,
    24.04809619,  24.6492986 ,  25.250501  ,  25.85170341,
    26.45290581,  27.05410822,  27.65531062,  28.25651303,
    28.85771543,  29.45891784,  30.06012024,  30.66132265,
    31.26252505,  31.86372745,  32.46492986,  33.06613226,
    33.66733467,  34.26853707,  34.86973948,  35.47094188,
    36.07214429,  36.67334669,  37.2745491 ,  37.8757515 ,
    38.47695391,  39.07815631,  39.67935872,  40.28056112,
    40.88176353,  41.48296593,  42.08416834,  42.68537074,
    43.28657315,  43.88777555,  44.48897796,  45.09018036,
    45.69138277,  46.29258517,  46.89378758,  47.49498998,
    48.09619238,  48.69739479,  49.29859719,  49.8997996 ,
    50.501002  ,  51.10220441,  51.70340681,  52.30460922,
    52.90581162,  53.50701403,  54.10821643,  54.70941884,
    55.31062124,  55.91182365,  56.51302605,  57.11422846,
    57.71543086,  58.31663327,  58.91783567,  59.51903808,
    60.12024048,  60.72144289,  61.32264529,  61.9238477 ,
    62.5250501 ,  63.12625251,  63.72745491,  64.32865731,
    64.92985972,  65.53106212,  66.13226453,  66.73346693,
    67.33466934,  67.93587174,  68.53707415,  69.13827655,
    69.73947896,  70.34068136,  70.94188377,  71.54308617,
    72.14428858,  72.74549098,  73.34669339,  73.94789579,
    74.5490982 ,  75.1503006 ,  75.75150301,  76.35270541,
    76.95390782,  77.55511022,  78.15631263,  78.75751503,
    79.35871743,  79.95991984,  80.56112224,  81.16232465,
    81.76352705,  82.36472946,  82.96593186,  83.56713427,
    84.16833667,  84.76953908,  85.37074148,  85.97194389,
    86.57314629,  87.1743487 ,  87.7755511 ,  88.37675351,
    88.97795591,  89.57915832,  90.18036072,  90.78156313,
    91.38276553,  91.98396794,  92.58517034,  93.18637275,
    93.78757515,  94.38877756,  94.98997996,  95.59118236,
    96.19238477,  96.79358717,  97.39478958,  97.99599198,
    98.59719439,  99.19839679,  99.7995992 , 100.4008016 ,
   101.00200401, 101.60320641, 102.20440882, 102.80561122,
   103.40681363, 104.00801603, 104.60921844, 105.21042084,
   105.81162325, 106.41282565, 107.01402806, 107.61523046,
   108.21643287, 108.81763527, 109.41883768, 110.02004008,
   110.62124248, 111.22244489, 111.82364729, 112.4248497 ,
   113.0260521 , 113.62725451, 114.22845691, 114.82965932,
   115.43086172, 116.03206413, 116.63326653, 117.23446894,
   117.83567134, 118.43687375, 119.03807615, 119.63927856,
   120.24048096, 120.84168337, 121.44288577, 122.04408818,
   122.64529058, 123.24649299, 123.84769539, 124.4488978 ,
   125.0501002 , 125.65130261, 126.25250501, 126.85370741,
   127.45490982, 128.05611222, 128.65731463, 129.25851703,
   129.85971944, 130.46092184, 131.06212425, 131.66332665,
   132.26452906, 132.86573146, 133.46693387, 134.06813627,
   134.66933868, 135.27054108, 135.87174349, 136.47294589,
   137.0741483 , 137.6753507 , 138.27655311, 138.87775551,
   139.47895792, 140.08016032, 140.68136273, 141.28256513,
   141.88376754, 142.48496994, 143.08617234, 143.68737475,
   144.28857715, 144.88977956, 145.49098196, 146.09218437,
   146.69338677, 147.29458918, 147.89579158, 148.49699399,
   149.09819639, 149.6993988 , 150.3006012 , 150.90180361,
   151.50300601, 152.10420842, 152.70541082, 153.30661323,
   153.90781563, 154.50901804, 155.11022044, 155.71142285,
   156.31262525, 156.91382766, 157.51503006, 158.11623246,
   158.71743487, 159.31863727, 159.91983968, 160.52104208,
   161.12224449, 161.72344689, 162.3246493 , 162.9258517 ,
   163.52705411, 164.12825651, 164.72945892, 165.33066132,
   165.93186373, 166.53306613, 167.13426854, 167.73547094,
   168.33667335, 168.93787575, 169.53907816, 170.14028056,
   170.74148297, 171.34268537, 171.94388778, 172.54509018,
   173.14629259, 173.74749499, 174.34869739, 174.9498998 ,
   175.5511022 , 176.15230461, 176.75350701, 177.35470942,
   177.95591182, 178.55711423, 179.15831663, 179.75951904,
   180.36072144, 180.96192385, 181.56312625, 182.16432866,
   182.76553106, 183.36673347, 183.96793587, 184.56913828,
   185.17034068, 185.77154309, 186.37274549, 186.9739479 ,
   187.5751503 , 188.17635271, 188.77755511, 189.37875752,
   189.97995992, 190.58116232, 191.18236473, 191.78356713,
   192.38476954, 192.98597194, 193.58717435, 194.18837675,
   194.78957916, 195.39078156, 195.99198397, 196.59318637,
   197.19438878, 197.79559118, 198.39679359, 198.99799599,
   199.5991984 , 200.2004008 , 200.80160321, 201.40280561,
   202.00400802, 202.60521042, 203.20641283, 203.80761523,
   204.40881764, 205.01002004, 205.61122244, 206.21242485,
   206.81362725, 207.41482966, 208.01603206, 208.61723447,
   209.21843687, 209.81963928, 210.42084168, 211.02204409,
   211.62324649, 212.2244489 , 212.8256513 , 213.42685371,
   214.02805611, 214.62925852, 215.23046092, 215.83166333,
   216.43286573, 217.03406814, 217.63527054, 218.23647295,
   218.83767535, 219.43887776, 220.04008016, 220.64128257,
   221.24248497, 221.84368737, 222.44488978, 223.04609218,
   223.64729459, 224.24849699, 224.8496994 , 225.4509018 ,
   226.05210421, 226.65330661, 227.25450902, 227.85571142,
   228.45691383, 229.05811623, 229.65931864, 230.26052104,
   230.86172345, 231.46292585, 232.06412826, 232.66533066,
   233.26653307, 233.86773547, 234.46893788, 235.07014028,
   235.67134269, 236.27254509, 236.87374749, 237.4749499 ,
   238.0761523 , 238.67735471, 239.27855711, 239.87975952,
   240.48096192, 241.08216433, 241.68336673, 242.28456914,
   242.88577154, 243.48697395, 244.08817635, 244.68937876,
   245.29058116, 245.89178357, 246.49298597, 247.09418838,
   247.69539078, 248.29659319, 248.89779559, 249.498998  ,
   250.1002004 , 250.70140281, 251.30260521, 251.90380762,
   252.50501002, 253.10621242, 253.70741483, 254.30861723,
   254.90981964, 255.51102204, 256.11222445, 256.71342685,
   257.31462926, 257.91583166, 258.51703407, 259.11823647,
   259.71943888, 260.32064128, 260.92184369, 261.52304609,
   262.1242485 , 262.7254509 , 263.32665331, 263.92785571,
   264.52905812, 265.13026052, 265.73146293, 266.33266533,
   266.93386774, 267.53507014, 268.13627255, 268.73747495,
   269.33867735, 269.93987976, 270.54108216, 271.14228457,
   271.74348697, 272.34468938, 272.94589178, 273.54709419,
   274.14829659, 274.749499  , 275.3507014 , 275.95190381,
   276.55310621, 277.15430862, 277.75551102, 278.35671343,
   278.95791583, 279.55911824, 280.16032064, 280.76152305,
   281.36272545, 281.96392786, 282.56513026, 283.16633267,
   283.76753507, 284.36873747, 284.96993988, 285.57114228,
   286.17234469, 286.77354709, 287.3747495 , 287.9759519 ,
   288.57715431, 289.17835671, 289.77955912, 290.38076152,
   290.98196393, 291.58316633, 292.18436874, 292.78557114,
   293.38677355, 293.98797595, 294.58917836, 295.19038076,
   295.79158317, 296.39278557, 296.99398798, 297.59519038,
   298.19639279, 298.79759519, 299.3987976 , 300.        ]

uB=[ 99.98653426, 100.08167306, 100.17311374, 100.25958093,
   100.33978246, 100.41255957, 100.47684726, 100.5318298 ,
   100.57679397, 100.61121483, 100.63472302, 100.64723246,
   100.64875752, 100.63942261, 100.61965391, 100.59006924,
   100.55125   , 100.50400345, 100.44927541, 100.3881855 ,
   100.32180205, 100.25119528, 100.17769381, 100.10261745,
   100.02711342,  99.95239448,  99.87964191,  99.81011973,
    99.74483432,  99.68464849,  99.63054014,  99.58332175,
    99.54357519,  99.51176442,  99.48821646,  99.47329584,
    99.46701643,  99.46923422,  99.47980614,  99.49841021,
    99.52459761,  99.55775153,  99.59714805,  99.64211631,
    99.69179796,  99.74524091,  99.80151269,  99.85963652,
    99.91865812,  99.97758813, 100.03542177, 100.09128508,
   100.14432589, 100.19371629, 100.2387236 , 100.27868336,
   100.31312381, 100.34156103, 100.36359832, 100.37909008,
   100.38791969, 100.39006242, 100.38559771, 100.37469258,
   100.35780539, 100.33521781, 100.30741818, 100.27494141,
   100.23850288, 100.19873655, 100.15621101, 100.11184301,
   100.06638511, 100.02053499,  99.97503887,  99.93057102,
    99.88800134,  99.84788008,  99.81072573,  99.77721235,
    99.74779038,  99.72285532,  99.70269535,  99.68745352,
    99.67749884,  99.67275594,  99.67320485,  99.67871321,
    99.68912467,  99.70421823,  99.72357347,  99.74682063,
    99.77349335,  99.80314859,  99.83516151,  99.86899252,
    99.90406837,  99.93979899,  99.97559266, 100.01079005,
   100.04492141, 100.07744203, 100.10783782, 100.13564258,
   100.16043641, 100.18198106, 100.19990729, 100.21400798,
   100.22411854, 100.23022783, 100.23225904, 100.2302473 ,
   100.22433725, 100.21472486, 100.20164571, 100.18527321,
   100.16602198, 100.14435026, 100.12052208, 100.09500183,
   100.06823012, 100.04071423, 100.01293172,  99.98516768,
    99.95803515,  99.93199557,  99.90735065,  99.8844886 ,
    99.86371362,  99.84540795,  99.82978734,  99.8170057 ,
    99.80725028,  99.80061713,  99.79720715,  99.79688025,
    99.79967166,  99.80547973,  99.81414446,  99.82545331,
    99.83908597,  99.85493417,  99.8726201 ,  99.89181044,
    99.91214485,  99.9332831 ,  99.95493041,  99.97664284,
    99.99808839, 100.01891462, 100.03885708, 100.05753678,
   100.07468708, 100.09008367, 100.10352574, 100.11482992,
   100.12376477, 100.13034884, 100.13450845, 100.13619737,
   100.13541399, 100.13221241, 100.12682665, 100.11923005,
   100.10962649, 100.09825937, 100.08534499, 100.04864641,
    99.90430881,  99.64946878,  99.29069358,  98.83582971,
    98.29400836,  97.67572474,  96.99242215,  96.25611589,
    95.47946416,  94.67587106,  93.8587414 ,  93.04138449,
    92.23725953,  91.45963867,  90.72100925,  90.03325696,
    89.40790208,  88.85489048,  88.38311422,  88.00051765,
    87.7130157 ,  87.52492677,  87.43911168,  87.45629056,
    87.57507609,  87.79278559,  88.1043273 ,  88.50298496,
    88.9806423 ,  89.52761799,  90.1330948 ,  90.78513874,
    91.47166698,  92.17956221,  92.89595669,  93.60817863,
    94.30362693,  94.97027203,  95.59753942,  96.17497568,
    96.69365726,  97.1458051 ,  97.52520437,  97.82662523,
    98.04662934,  98.18309306,  98.23535297,  98.2042004 ,
    98.09169502,  97.90122823,  97.63758201,  97.30630617,
    96.91394266,  96.46828218,  95.97751915,  95.45032275,
    94.8959851 ,  94.32418924,  93.74466837,  93.16692549,
    92.60079806,  92.05573576,  91.54042896,  91.06339743,
    90.63253096,  90.25465351,  89.93562728,  89.6808057 ,
    89.4937926 ,  89.37714526,  89.33256739,  89.35995474,
    89.45800413,  89.62429491,  89.85518894,  90.14553542,
    90.48962344,  90.8805052 ,  91.31056924,  91.77160161,
    92.25488018,  92.75164254,  93.25273707,  93.74940486,
    94.23305206,  94.69530709,  95.1285695 ,  95.5260641 ,
    95.8812745 ,  96.18902209,  96.44491797,  96.64553729,
    96.78845672,  96.87232162,  96.8967427 ,  96.86251931,
    96.77111358,  96.62505589,  96.42805632,  96.18388364,
    95.89757101,  95.57465825,  95.22111892,  94.8432749 ,
    94.44767309,  94.04157814,  93.63170464,  93.2249955 ,
    92.82834476,  92.44849089,  92.09140654,  91.76305323,
    91.46895616,  91.21345831,  91.00066618,  90.83391114,
    90.71554542,  90.64691208,  90.62894321,  90.66127585,
    90.74255581,  90.87104177,  91.04379288,  91.25710948,
    91.50667587,  91.78768371,  92.09466731,  92.42182264,
    92.76309586,  93.11228229,  93.46313582,  93.80944239,
    94.14533594,  94.46507488,  94.76332903,  95.03544938,
    95.27710264,  95.48465179,  95.6551468 ,  95.78629182,
    95.87659789,  95.9250558 ,  95.93178796,  95.89729763,
    95.82280671,  95.71024209,  95.5623565 ,  95.38191405,
    95.17253728,  94.93849355,  94.68387944,  94.41329939,
    94.13155232,  93.84377232,  93.55470582,  93.26918651,
    92.99247182,  92.72880346,  92.48257514,  92.25782755,
    92.05828014,  91.8868803 ,  91.7462143 ,  91.63873252,
    91.56542497,  91.52738649,  91.52488756,  91.55748364,
    91.62406043,  91.72307768,  91.85247591,  92.00940951,
    92.19081798,  92.39319389,  92.61269136,  92.84509476,
    93.08630313,  93.33189637,  93.57751619,  93.81888188,
    94.05186462,  94.27259555,  94.47736474,  94.66297681,
    94.8265473 ,  94.96553219,  95.07801988,  95.16256978,
    95.21812179,  95.24411227,  95.2408599 ,  95.20867141,
    95.1485208 ,  95.06208628,  94.95119453,  94.81804092,
    94.66516524,  94.49571636,  94.31265873,  94.11914527,
    93.91897445,  93.71550306,  93.51217726,  93.31244712,
    93.11986403,  92.93752605,  92.76819215,  92.61510129,
    92.48035577,  92.36604394,  92.27392098,  92.20540731,
    92.16126541,  92.14180151,  92.14747952,  92.17743449,
    92.23096692,  92.30688715,  92.40354847,  92.51890754,
    92.65063274,  92.79641514,  92.95331361,  93.11846553,
    93.28888869,  93.46159333,  93.6334053 ,  93.80143226,
    93.96289789,  94.11497856,  94.25522981,  94.38143854,
    94.4916991 ,  94.58420249,  94.65779998,  94.71153724,
    94.74471733,  94.75706132,  94.74888757,  94.72051968,
    94.67262497,  94.60663286,  94.52381663,  94.42577418,
    94.31441099,  94.19199038,  94.06066833,  93.92261221,
    93.78068562,  93.63715898,  93.49444836,  93.35503965,
    93.22139146,  93.09563517,  92.9796258 ,  92.87566342,
    92.78507236,  92.70922502,  92.64935923,  92.60631804,
    92.58052924,  92.57210145,  92.58129095,  92.60736273,
    92.64968607,  92.70748932,  92.77942775,  92.8640148 ,
    92.95956764,  93.06446429,  93.1765183 ,  93.29369583,
    93.41410692,  93.53542317,  93.65554299,  93.77239392,
    93.88411298,  93.98870699,  94.08451475,  94.1701059 ,
    94.24411676,  94.30537099,  94.353075  ,  94.38675501,
    94.40574446,  94.41016857,  94.40022701,  94.37618954,
    94.33865756,  94.28853245,  94.22700936,  94.15495328,
    94.07405165,  93.98586319,  93.89181979,  93.79365209,
    93.69314424,  93.59217024,  93.49213985,  93.39507319,
    93.30255495,  93.21602397,  93.13683001,  93.06640537,
    93.00576061,  92.95573319,  92.9171527 ,  92.89055335,
    92.8761335 ,  92.87388039,  92.88398622,  92.90583304,
    92.93892114,  92.982557  ,  93.03591928,  93.09768756,
    93.16690232,  93.24223557,  93.322213  ,  93.40533565,
    93.49024623,  93.57539129,  93.65922605,  93.74039755,
    93.81757406,  93.88941667,  93.95465989,  94.01255999,
    94.06202327,  94.10233726,  94.13297682,  94.15362447]
# Plot uB

#find line of best fit
'''a, b = np.polyfit(t, uB, 1)
plt.plot(a, uB*, label='uB')


plt.legend()'''



# Importing necessary libraries
import numpy as np
import matplotlib.pyplot as mp

t = list(np.around(np.array(t),2))
uB = list(np.around(np.array(uB),2))
# X-Coordinates
x = t

# Polynomial equation
y = uB
# Using polyfit func with 4 degrees
p = np.poly1d( np.polyfit(x, y,8) )
t1 = np.linspace(0, 1, 250)

# Plotting our polynomial function
mp.plot(t, p(t), '-')
mp.show()`

I tried polyfit splines and curve fit
python polynomials
1个回答
0
投票

您调用的算法正在尽其所能。通过做

# Using polyfit func with 4 degrees                                                                                   
p = np.poly1d(np.polyfit(t, uB, deg=4))                                                                               
t1 = np.linspace(0, 300, 2500)                                                                                        
                                                                                                                      
# Plotting our polynomial function                                                                                    
plt.scatter(t, uB, label='data')                                                                                      
plt.plot(t1, p(t1), '-', label='fit')                                                                                 
plt.legend()                                                                                                          
                                                                                                                      
plt.show()

你得到这个:

50 次的多项式拟合也好不了多少:

在将数据输入数值算法之前,您应该验证该算法是否适合您手头的问题。在你的例子中,在值 t = 100 附近,你有一个陡峭的斜率,这会破坏大多数适用于 smooth 函数的算法。阅读吉布斯现象以了解概述。

顺便说一句,上面的代码让我使用 deg = 3 进行拟合,因此您可能需要仔细检查您的代码是否有问题。

为了拟合您的数据,最好的选择是分段多项式或傅里叶拟合。

© www.soinside.com 2019 - 2024. All rights reserved.