Why does car run hot when heat or air is on for 20 minutes on 2007 Mazda Mazda3

Thermostat was replaced when car overheated after check engine light kept coming on. Car now goes to hot after about 20 minutes with heat or air conditioner on. Drops fairly slowly to normal when turned heat/air turned off.

Asked by for the 2007 Mazda Mazda3