Smoothing Data, Box-car method

I am trying to smooth my data which I am visualising from the graph, more of a boxcar method, but I am not using the boxcar module.
The thing is, it take values which I do not have, before the first value, so now the graph that I am getting is unreal on the first and last of it, for instance as attached on the photo.
This is my code which have about 7000+ values:

Boxcar smoothing function using ‘same’ mode

def boxcar_smooth(data, window_size):
window = np.ones(window_size) / window_size
smoothed_data = np.convolve(data, window, mode=‘same’)
return smoothed_data

Apply boxcar smoothing

window_size = 300
smoothed_density = boxcar_smooth(all_density, window_size)

Please read the pinned thread to understand how to format code properly for the forum.

Aside from that, the photo you wanted doesn’t seem to actually be attached.

Yes, I see the picture now.

Yes, that’s how np.convolve works by default:

https://numpy.org/doc/stable/reference/generated/numpy.convolve.html

For what you want, I think passing mode='valid' should work. (Or you could slice out the parts you don’t want at the ends.) When you graph the data, you’ll need to make sure it lines up properly with the X axis, by calculating right time for the first smoothed data point.