a, Schematic of behavioral setup. b, 2-D path of a male courting a pseudo-randomly moving target in closed-loop. c, Angular position of the target relative to males expressing CsChrimson in P1 neurons or wild-type animals during tethered closed-loop courtship. d, Example of a courting male displaying turning (middle) and wing-extensions (bottom) to the visual target in open-loop. e, Example of a male’s turning throughout a courtship trial. Each row consists of three stimulus cycles, with the target angle shown at top. Red line indicates 3-second P1 activation; black bar indicates when the visual stimulus is oscillating. f, Tracking Index (see Methods) for 10 flies following optogenetic activation of P1 neurons. Top trace is the same animal as in (e). g-h, Same as e-f but for spontaneously initiated courtship trials. i, Schematic of P1 neurons in the male brain. j, Example of P1 neuron activity (ΔF/F0) and Tracking Index of a male during a courtship trial. k, Zoomed-in view of the onset of courtship in (j). l, Activity of P1 neurons (average ΔF/F0) versus Tracking Index. m, Distribution of P1 activity (ΔF/F0) before courtship was initiated, during periods when males were temporarily disengaged, and during active courtship pursuit (Tracking Index > 0.3). n,o Activity of P1 neurons (average ΔF/F0) versus the male’s linear speed (n) or angular velocity (n). Shaded line plots are mean±s.e.m.; thin lines denote individual animals; n.s. indicates p > 0.05; **** indicates p <0.0001. Details of statistical analyses and sample sizes are given in Supplementary Table 1.