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
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()
平滑曲線函數
# 平滑函數的作用是將每一個點的值變為 上一個節點*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
print(smooth_curve(y))
plt.plot(x, smooth_curve(y), 'r-') plt.show()
我們可以看到,使用平滑函數之后,曲線就變的比較平滑了
本系列博客對應課程位置:
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