I find what you are saying really hard to understand. Let me try to decode it. Let me know if my decoding is correct.
Say you have a bandpass filter and you place it on 40 discretely sampled points from a sinusoid with a period of 40.
This makes little sense. What I think you mean is: I have a filter with impulse response $h(n)$ that I am going to apply to a sinusoidal signal of length 40 samples.
Why is the bandpass filter (green curve on the image) only "in phase" with the sinusoid if you use a window length (impulse response length) for the filter of 1/4th the period of the sinusoid (here 40/4=10)?
Again, I'm finding it hard to fathom what you mean, but I interpret this as: If my impulse response $h(t)$ is of length 10 samples, then the output of my filter is a sinusoid that is in phase with the input. When I use an impulse response of a different length (9 or 11) then the phase is incorrect.
For a start, your filter output will not generally have precisely the same phase as the filter input. If your filter is a linear phase FIR filter, then as @MattL says, the filter will hvae a group delay of $(N-1)/2$ samples --- 4.5 samples in the case of a length 10 filter.
Trying to reproduce your diagram (
scilab code below) shows this effect: the green plot is the original, the red plot is the output of a length 9 filter (which has a delay of 4 samples), the black plot is the output of a length 10 filter (delay 4.5), and the blue is the output for a length 11 filter (delay of 5).
P = 40;
x = sin(2*%pi*[0:79]/P)
h10 = ones(1,10)/10;
h09 = ones(1,9)/9;
h11 = ones(1,11)/11;
y09 = filter(h09,1,x);
y10 = filter(h10,1,x);
y11 = filter(h11,1,x);