【问题标题】:How to make a noisey signal look smooth in matplotlib?如何在 matplotlib 中使嘈杂的信号看起来平滑?
【发布时间】:2019-11-04 21:44:30
【问题描述】:

所以,我有这个锯齿状信号,我想知道 matplotlib 中是否有一些参数可以使其绘图更平滑。 我见过 TensorBoard 有一个滑块来控制锯齿状随机曲线图的平滑度。

PS:我想要一些简单的代码技巧,而不是花哨的信号处理技术。

【问题讨论】:

    标签: python matplotlib smoothing


    【解决方案1】:

    您可以像这样使用 pandas 轻松过滤数据:

        import numpy as np
    
        elements = 100
        noise = np.random.normal(0,10,elements)
        signal = []
        for i in range(elements):
            signal.append(i+noise[i])
    
        import pandas as pd
    
        #if you run jupyter-notebook
        %matplotlib inline
        #load data into pandas
        series = pd.Series(signal)
        series.plot()
    
        #you can filter with rolling mean / median, etc in a window of 5 like:
        filtered_mean = series.rolling(5).mean()
        filtered_mean.plot()
    
        filtered_median = series.rolling(5).median()
        filtered_median.plot()
    

    注意:如果您开始使用 pandas series/dataframes,pandas 可以直接使用 matplotlib 进行绘图。这可以让您的生活更轻松。

    【讨论】:

      【解决方案2】:

      我只知道 python 的线性趋势(使用 seaborn regplot 函数)。

      对于更复杂的东西,您将进行一点信号处理。例如,您可以这样做:

      import numpy as np
      import matplotlib.pyplot as plt
      
      degree = 10
      # Let's say you need a trend line for the points (x,y)
      z = np.polyfit(x, y, degree)
      p = np.poly1d(z)
      
      plt.plot(x,y,'b')
      plt.plot(x,p(x),'r')
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        • 2014-05-19
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        相关资源
        最近更新 更多