Ulcerative colitis (UC) is a severe threat to humans worldwide. Single-cell RNA sequencing (scRNA-seq) can be used to screen gene expression patterns of each cell in the intestine, provide new insights into the potential mechanism of UC, and analyze the development of immune cell changes. These findings can provide new ideas for the diagnosis and treatment of intestinal diseases. In this study, bioinformatics analysis combined with experiments applied in dextran sulfate sodium (DSS)-induced colitis mice was used to explore new diagnostic genes for UC and their potential relationship with immune cells. We downloaded microarray datasets (GSE75214, GSE87473, GSE92415) from the Gene Expression Omnibus and used these datasets to screen differentially expressed genes (DEGs) and conduct Weighted Gene Co-expression Network Analysis (WGCNA) after quality control. The hub genes were screened, and ROC curves were drawn to verify the reliability of the results in both training set (GSE75214, GSE87473, GSE92415) and validation cohort (GSE87466). Also, we explored the relation of diagnostic genes and immune cells by CIBERSORT algorithm and single-cell analysis. Finally, the expression of hub genes and their relation with immune cells were verified in DSS-induced colitis mice. Diagnostic genes (ANXA5, MMP7, NR1H4, CYP3A4, ABCG2) were identified. In addition, we found these five genes firmly related to immune infiltration. The DSS-induced colitis mice confirm that the expression of ANXA5 mainly increased in the intestinal macrophages and had a strong negative correlation with M2 macrophages, which indicated its possible influence on the polarization of macrophages in UC patients. We identified ANXA5, MMP7, NR1H4, CYP3A4, and ABCG2 as diagnostic genes of UC that are closely related to immune infiltration and ANXA5 maintains a negative correlation with M2 macrophages which indicated its possible influence on the polarization of macrophage in UC patients.