matplotlib庫疑難問題---2、將曲線平滑


matplotlib庫疑難問題---2、將曲線平滑

一、總結

一句話總結:

曲線平滑的原理非常簡單,將每一個點的值變為 上一個節點*0.8+當前節點*0.2
# 平滑函數的作用是將每一個點的值變為 上一個節點*0.8+當前節點*0.2
def smooth_curve(points, factor=0.8):   
    smoothed_points = []   
    for point in points:     
        if smoothed_points:       
            previous = smoothed_points[-1]   
            # 上一個節點*0.8+當前節點*0.2
            smoothed_points.append(previous * factor + point * (1 - factor))     
        else:
            # 添加point
            smoothed_points.append(point)   
    return smoothed_points

 

 

二、將曲線平滑

博客對應課程的視頻位置:2、將曲線平滑-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/372

 


import numpy as np import matplotlib.pyplot as plt
In [2]:
x = np.linspace(1,4,80) y=[] for i in range(80): y.append(i+3*(-1)**i) print(x) print(y) plt.plot(x, y, 'r-') plt.show() 
[1.         1.03797468 1.07594937 1.11392405 1.15189873 1.18987342
 1.2278481  1.26582278 1.30379747 1.34177215 1.37974684 1.41772152
 1.4556962  1.49367089 1.53164557 1.56962025 1.60759494 1.64556962
 1.6835443  1.72151899 1.75949367 1.79746835 1.83544304 1.87341772
 1.91139241 1.94936709 1.98734177 2.02531646 2.06329114 2.10126582
 2.13924051 2.17721519 2.21518987 2.25316456 2.29113924 2.32911392
 2.36708861 2.40506329 2.44303797 2.48101266 2.51898734 2.55696203
 2.59493671 2.63291139 2.67088608 2.70886076 2.74683544 2.78481013
 2.82278481 2.86075949 2.89873418 2.93670886 2.97468354 3.01265823
 3.05063291 3.08860759 3.12658228 3.16455696 3.20253165 3.24050633
 3.27848101 3.3164557  3.35443038 3.39240506 3.43037975 3.46835443
 3.50632911 3.5443038  3.58227848 3.62025316 3.65822785 3.69620253
 3.73417722 3.7721519  3.81012658 3.84810127 3.88607595 3.92405063
 3.96202532 4.        ]
[3, -2, 5, 0, 7, 2, 9, 4, 11, 6, 13, 8, 15, 10, 17, 12, 19, 14, 21, 16, 23, 18, 25, 20, 27, 22, 29, 24, 31, 26, 33, 28, 35, 30, 37, 32, 39, 34, 41, 36, 43, 38, 45, 40, 47, 42, 49, 44, 51, 46, 53, 48, 55, 50, 57, 52, 59, 54, 61, 56, 63, 58, 65, 60, 67, 62, 69, 64, 71, 66, 73, 68, 75, 70, 77, 72, 79, 74, 81, 76]

平滑曲線函數

In [3]:
# 平滑函數的作用是將每一個點的值變為 上一個節點*0.8+當前節點*0.2
def smooth_curve(points, factor=0.8): smoothed_points = [] for point in points: if smoothed_points: previous = smoothed_points[-1] # 上一個節點*0.8+當前節點*0.2 smoothed_points.append(previous * factor + point * (1 - factor)) else: # 添加point smoothed_points.append(point) return smoothed_points 
In [4]:
print(smooth_curve(y)) 
[3, 2.0000000000000004, 2.6000000000000005, 2.0800000000000005, 3.064, 2.8512, 4.080959999999999, 4.064767999999999, 5.451814399999998, 5.5614515199999985, 7.049161215999999, 7.239328972799999, 8.791463178239999, 9.033170542592, 10.6265364340736, 10.901229147258881, 12.520983317807104, 12.816786654245684, 14.453429323396547, 14.762743458717237, 16.410194766973788, 16.72815581357903, 18.382524650863225, 18.70601972069058, 20.36481577655246, 20.691852621241967, 22.353482096993574, 22.682785677594858, 24.346228542075885, 24.67698283366071, 26.341586266928566, 26.673269013542853, 28.338615210834284, 28.67089216866743, 30.336713734933944, 30.669370987947154, 32.33549679035772, 32.66839743228618, 34.33471794582894, 34.66777435666315, 36.33421948533052, 36.66737558826442, 38.33390047061154, 38.66712037648923, 40.33369630119138, 40.66695704095311, 42.333565632762486, 42.666852506209985, 44.33348200496798, 44.66678560397438, 46.3334284831795, 46.66674278654361, 48.33339422923489, 48.66671538338792, 50.33337230671034, 50.66669784536827, 52.33335827629462, 52.666686621035694, 54.33334929682855, 54.66667943746284, 56.333343549970266, 56.66667483997621, 58.33333987198097, 58.66667189758478, 60.33333751806782, 60.66667001445426, 62.33333601156341, 62.66666880925073, 64.33333504740058, 64.66666803792047, 66.33333443033638, 66.6666675442691, 68.33333403541528, 68.66666722833222, 70.33333378266578, 70.66666702613261, 72.33333362090609, 72.66666689672488, 74.3333335173799, 74.66666681390392]
In [5]:
plt.plot(x, smooth_curve(y), 'r-') plt.show() 

我們可以看到,使用平滑函數之后,曲線就變的比較平滑了

In [ ]:
 
 

本系列博客對應課程位置:
1、解決中文亂碼問題-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/371
2、將曲線平滑-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/372
3、matplotlib繪圖核心原理-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/373
4、畫動態圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/374
5、保存動態圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/375
6、顯示圖片-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/376

7、去掉刻度和邊框-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/383

8、幾個點畫曲線-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/384

9、畫箭頭(綜合實例)-1-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/391

9、畫箭頭(綜合實例)-2-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/392

10、畫直方圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/393

11、畫動態直方圖-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/43/394


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM