I am doing some research for a book project I am working on.
Ultimately, it comes down to plant pigments called anthocyanins. Usually, a plant does this to protect it against sun, or just because of natural selection. Sometimes, this can help deter animals to some extent, as well.