Pollution of farmland by heavy metals threatens food security and human health. In addition, heavy metals in soil could infiltrate into groundwater to influence the water quality and safety of drinking water. However, the relationship between heavy metal pollution in soil and groundwater is still not clear. In this study, we investigated the soil and groundwater in the Guanzhong Plain region, which is a significant grain production base in China, and determined the spatial distributions, ecological risk, sources, and migration fates of heavy metals (As, Cd, Cr, Cu, Ni, Pb, and Zn). The results showed that the mean values (0-20cm) in the soil were 19.57mgkg-1 for As, 0.71mgkg-1 for Cd, 69.65mgkg-1 for Cr, 21.97mgkg-1 for Cu, 28.67mgkg-1 for Ni, 17.54mgkg-1 for Pb, and 73.77mgkg-1 for Zn, and the corresponding mean values in groundwater were 1.2, 0.04, 4.69, 0.15, 0.07, 0.3, and 3.6μg L-1, respectively. The mean values for As, Cd, Cr, Pb, and Zn in soil exceeded the background values, and the mean values for As, Cd and Pb exceeded those in groundwater. Positive matrix factorization models identified five sources (fertilizers and organic fertilizers, natural sources, pesticides and herbicides, industrial activities, and sedimentation caused by transportation) for heavy metal pollution in soil and four sources (industry activity, atmospheric sedimentation caused by transportation, natural sources, and agriculture) for heavy metal pollution in groundwater. The soil particle composition and soil organic carbon content were important factors that affected the vertical distribution of heavy metals in the soil. The migration modes (convection and diffusion) were not found for all heavy metals. These results help to understand the relationships between heavy metals in soil and groundwater in farmland ecosystems regionally.