First, I would put Madrid and Andalucia first and Barcelona last just because of the heat. Also check for local ferias (festivals) in Andalucia. Off the top of my head, there's a spring festival in Sevilla, patios in Cordoba and horses in Jerez.
Second, it's much better to count in nights, which gives you a better sense of how much time you really have in each place.
I love Barcelona, I could (and have) filled twice that much with only a couple of return visits to sights and 3-4 day trips. I would go back in a heartbeat. There are great walking tours, and day tours out of the city, as well as a number of easy day trips on your own by train. Spend a couple hours browsing the official TI website for all the possibilities.
I spent 4N in Granada and felt that 3 would have been enough but since your trip is a lot longer, 4N seems good.
I was in Malaga once, for the Holy Week celebrations. 4N and I saw most of the sights (nice enough) in my "spare time." It may be a good place if you want some beach time.
Sevilla is another city with much to see and interesting to wander. Cordoba is my favorite Spanish city, so for me, 3N gives you just enough time to scratch the surface. OTOH Toledo which is somewhat similar (don't ask me why I think so, I can't really explain it), 3N is fine.
I haven't been to Segovia, but many seem to think it's fine as a day trip. You might consider just a single night with a small overnight bag. There's left luggage at the Madrid Atocha train station.
5N in Madrid is okay, especially if you switch and start there. If you love European painting, you'll want 2 full days for the 3 major art museums, with your first full day seeing some other sights.