Calibration of ptychographic setup properties, such as the sample-detector axial distance, is crucial for quantitative and high-resolution reconstructions. This study proposes a calibration scheme based on the degree of coherence (i.e., purity) of the probe in mixed-state ptychography. Compared to established sharpness metric-based methods, it is significantly more robust and applicable to arbitrary samples. A numerical study and validations with various experimental datasets have been conducted to verify the performance of the proposed method, including the application to high-contrast samples and weakly-scattering biological samples. This calibration scheme can significantly enhance the image quality, the precision of wavefront sensing, and the reliability of quantitative analysis for ptychography. Potentially, we believe this optimization method can find further applications in other areas of computational imaging.