Search code examples
pythonmatplotlibtensorboard

How to get smooth mean curves in Pyplot


So Tensorboard has this nice smoothing feature where you can plot the original curves in shaded colors, and a smooth version on top in solid color: enter image description here

How can I do this with Pyplot? This feature is key to spot the trends with the smoothed curves and the variance with the shaded original curves. Tensorboard is quite limited though, so I'd like to recreate the look in matplot lib. Any ideas?

Note I don't want to "interpolate" between the data points, there's too much variance.


Solution

  • You can use the rolling-object of pandas. With this you can define a window of certain size which rolls over your data and you can use it then for calculating mean, sum or whatever.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    t = np.linspace(0, 1.5*np.pi, 2500)
    y = np.sin(t**2)+np.random.random(2500)*.6
    df = pd.DataFrame(y)
    plt.plot(df[0], 'lightblue', df[0].rolling(10).mean(), 'b')
    

    enter image description here