It's worth checking airfare from your preferred US airport(s) to Madrid, Malaga, Seville and Granada to see what looks viable. The round-trip fares can be a guide to which multi-city fares to check, because they are often around the average of the two round-trip fares. From my origin, Madrid was about $500 cheaper each way than any of the other southern options, but your home airport(s) may be totally different.
It's likely to be quite warm, or worse, in the hottest places in the south (which include Seville and Cordoba) in early May or late September. I'd try to put those stops early in a May trip and late in a trip beginning in late September, and even the latter will be no guarantee. You can explore actual, historical, day-by-day weather statistics for most places you'd want to go on the website timeanddate.com; you'll usually have access to data for the most recent ten years. Seville's the hottest, so that's the one I'd concentrate on.
Seville May 2022
Seville September 2022
Seville October 2022 (some really rough weather that month)
You should take a look at more than one year since weather varies so much. You can change the month and year via the pull-down menu just above the graph at the right. To look at statistics for other cities, use the Search box near the top right of the screen.
As geovagriffith mentioned there are additional very good stops in the area of Spain you're thinking about. Three weeks, believe it or not, will be tight because of the number of cities there with lots of, or time-consuming, attractions. Since you mentioned an interest in smaller places, I'll include some for you in this list:
- Madrid (Toledo, Segovia, Salamanca, Cuenca, Alcala de Henares--all worth a couple of nights or more, but close enough to be day trips if you must). Days needed in Madrid depend on how you feel about its big-deal art museums and your plans for day trips. If neither interest you, 3 nights might be OK.
- Seville (Carmona's a nice day trip). Large city with a lot of sights. I'd stay at least 4 nights if not taking any side trips.
- Cordoba (deserves a couple of nights or more, but many people day trip there from Seville, which means an extra night in Seville)
- Granada (Priego de Cordoba's a good day trip, and outdoorsy people enjoy the Alpujarras). I'd want at least 3 nights here; the Alhambra alone can take well over half a day.
- Cadiz: historic port city that doesn't get many American tourists (good side trips are the white town of Vejer de la Frontera and Jerez de la Frontera; the latter has sherry bodegas and horse shows). You might get back with 2 nights here if not taking any day trips.
- Malaga: nice coastal city with Alcazar and some interesting museums. Three nights here would be a nice coastal break if you're not seeing Cadiz.
- Ronda and the often-mentioned white villages/towns of Arcos de la Frontera, Grazalema and Zahara de la Sierra are sprinkled across an inland area between Malaga and Jerez/Cadiz. Arcos is an easy day trip from Jerez de la Frontera. Ronda is larger and has more public-transit connections than Grazalema and Zahara. Seeing more than one of those four places via public transportation takes patience and more time than you'd probably have if your trip is only 3 weeks. There are tours from Seville, or you could rent a car for a day or two and see some of those places on your way to or from Malaga or Granada.
I want to make it clear that I'm not suggesting what I've listed above is a 3-week trip; it's just a menu of options. This is an extremely sight-rich area.