Fully commented. Legend needs shrinking on plot3
Write directly to the PNG device