The field of education is paying more and more attention to the fundamental task of education by establishing morality, and ideological and political education has become a major project in which all the teaching and learning links cooperate with each other and are accomplished in a concerted manner. This study explores the method of organic integration of ideological and political education and teaching and data visualization technology to enhance the effect of ideological and political teaching. Firstly, the method of portrait construction is introduced, combined with the student behavior dataset, and the student behavior data is preprocessed. Using the user portrait construction method as a hub, a gradient boosting decision tree model was used to predict the students’ Civics learning performance. The improved K-prototypes clustering algorithm was used to categorize student groups, which facilitated teachers to develop targeted learning strategies. Finally, group portraits and feature labels are extracted from the students to further help teachers accurately determine the types of student groups and carry out personalized teaching. The classroom teaching model in this paper classifies students into four categories with obvious behavioral characteristics, which increases teachers’ understanding of students, and the model not only improves students’ academic performance in Civics, but also significantly improves students’ level of course Civics and increases students’ classroom active response rate by 19.625%. The Civics education data visualization technology proposed in this paper reveals the rules of Civics education and improves teachers’ work efficiency.