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