A criterion for subharmonicity of a function of the spectrum