Shakespeare wrote the Scottish play to honor the Scottish James I, who published a treatise on witchcraft and was fascinated with the occult. Below are from "Real Macbeth and His Times":
It seems to me impossible to come to any decision. Shakespeare's Macbeth is a piéce d'occasion, written for the accession of James, who had hitherto been King of Scotland. The plot was ready-made....
The accession of James I was like a demonstration of the curse of unfruitfulness and the blessings of continuous generation. And the action of Shakespeare's Macbeth is based on this same contrast.
Macbeth is based upon actual events, but only very loosely. The real Macbeth ruled as king of Scotland from 1040-1057. He did overthrow Duncan, but Duncan was a poor king to the point of being a tyrant and a military failure. After his death, Macbeth was elected as king of Scotland. The Elizabethan English could not comprehend a ruler whose power was not hereditary, so they saw Macbeth as a tyrant. Lady Macbeth, whose real name was Gruach, was a granddaughter of another king, Kenneth III. In fact, Kenneth had been murdered by Duncan's grandfather. Malcom and Donald Ban (Donalbain) were sons of Duncan and Malcom did become king after the death of Macbeth. The character of Macduff is not based on any historical person. Though Banquo is said to be the ancestor of James I, he is first introduced in the 16th century. The Earl Siward was a real person whose son died in a battle against Macbeth.
The primary source Shakespeare used was Holinshed's Chronicles. You must find this source:
Shakespeare got his story from Raphael Holinshed’s Chronicles. Holinshed spends a lot of time on the incident in which Malcolm (who became a popular king) tests Macduff by pretending to be mean when he is really nice.
Holinshed talks about the murder of King Duff by Donwald in the century before Macbeth.
According to Holinshed, Donwald was nagged by his wife until he did the evil deed. Shakespeare adapted this for Macbeth.
In 1020, Findlaech was killed and succeeded by his nephew Gillacomgain. In 1032, Gillacomgain and fifty other people were burned to death in retribution for the murder of Findlaech, probably by Macbeth and allies.
The historical Mrs. Macbeth was not named “Lady”, but “Gruoch” (GROO-och). She was the daughter of a man named Biote (Beoedhe), who was in turn the son of King Kenneth III “the Grim” who Malcolm II had killed to become king. (Some say that Biote was the son of Kenneth II instead.) She was originally married to Gillacomgain. Their son was Lulach the Simple (i.e., stupid; no, Lady Macbeth didn’t brain him.) After Macbeth killed Gillacomgain, he took his widow Gruoch for his own wife, and raised Lulach as their stepson. What a guy!