Hardware electronic compass built in your device needs sometimes calibration. Either it does not show the bearing correctly or some application prompts you about the necessity of the compass calibration.
Google as the manufacturer of Android OS has this official instruction:
open the Google Maps app and then follow these directions:
tilt your phone forward and back
move it side to side
and then tilt left and right
you may need to repeat the steps until your compass is calibrated.
It does not have to be this complicated though. It is enough when you move your phone in the shape of 8: