  We compute the linear polarisation during the afterglow phase of gamma-ray
bursts, for both on-axis and off-axis observers. We use numerical simulations
of the deceleration of a relativistic jet, and compute the polarisation by
post-processing the results of the numerical simulations. In our simulations,
we consider a magnetic field that is chaotic in the plane of the shock, in
addition to a magnetic field component that is parallel to the shock velocity.
While the linear polarisation computed for on-axis observers is consistent with
previous analytical estimates, we found that lateral expansion, which is
accurately handled in our simulations, plays a crucial role in determining the
linear polarisation for off-axis observers. Our results show that the off-axis
linear polarisation, as seen by off-axis observers, exhibits a single peak, in
contrast to the two peaks inferred by previous analytical studies. The maximum
polarisation degree is 40\% at an observing angle $\theta_{\rm obs}=0.4$ rad,
and it decreases as the observing angle increases, which is opposite to what
predicted by analytical models, where polarisation increases with larger
observing angles. From the upper limit of 12\% in the linear polarisation
obtained at 244 days for the GRB 170817A, we also infer an anisotropy factor of
$B_\parallel/B_\perp = 0.5-0.9$, consistent with the post-shock magnetic field
being amplified by turbulence.
