Each build is different. If I am building a closed up canopy I tend to glue before painting. With open canopy builds I have done both paint the canopy separate and on the plane. Sometimes it can be a great way to cover the cockpit during the paint process.
I usually will roll blue tack into very small worms, place on area where canopy would touch fuselage when closed and then press the canopy in place. This secures the canopy for painting. Of course I will mask the canopy prior to tacking it in place.
The blue tack sandwiched between the canopy and fuselage seals the cockpit from overspray. Any blue tack the squeezed out I can remove using extra blue tack or use a toothpick to press back in or out of the way.