Why did Shakespeare write Macbeth for King James?
James I took Shakespeare's theater company under his patronage, and its name changed from "Lord Chamberlain's Men" to "The King's Men." Shakespeare played his political hand cleverly by writing a play that enhanced the moral virtues of Banquo, whom King James called his ancestor.
Shakespeare based his historical plays on Holinshed's Chronicles, in which Banquo is considered a historical figure. Later studies disputed such belief, but in the 17th century no one doubted that the House of Stuart descended from the first High Steward of Scotland who, in turn, was believed to have been Banquo's great-grandson. As you know, James was already king of Scotland when he ascended the English throne.
In order to please his new benefactor -remember that Shakespeare had also found favor with Elizabeth I, James's predecessor- the Bard altered the information provided by Holinshed. While the Chronicles recorded that Banquo had been Macbeth's accomplice in the murder of Duncan and in the subsequent usurpation of the Scottish throne, Shakespeare portrayed him as an innocent spectator first and as a victim later.
It stands to reason that James I would not have appreciated being associated to a murderer of kings. However, Shakespeare did include a hint that pointed to Banquo's devious behavior. After Duncan's death, Banquo is shown siding with Macbeth, when he should have supported Malcolm if he had been a loyal subject.
It is not clear why he did this. King James and his court did not notice the contradiction. If they had, Shakespeare's career would probably have ended then and there.