代码如下:
from tkinter import *
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
def function1():
courses = np.array(['语文', '数学', '英语', '物理', '化学', '生物'])
scores = np.array([[95, 75, 86],
[96, 93, 76],
[63, 85, 93],
[91, 88, 67],
[86, 76, 87]])
data_length = len(scores)
angles = np.linspace(0, 2 * np.pi, data_length, endpoint=False)
scores = np.concatenate((scores, [scores[0]]))
angles = np.concatenate((angles, [angles[0]]))
plt.polar(angles, scores, 'o-', linewidth=3)
plt.thetagrids(angles * 180 / np.pi, courses, fontproperties='simhei')
plt.legend(['高二1班', '高二2班', '高二3班'], loc=(0.94, 0.80), labelspacing=0.1)
plt.show()
def function2():
courses = np.array(['语文', '数学', '英语', '物理', '化学', '生物'])
scores1 = np.array([95, 96, 85, 63, 91, 86])
scores2 = np.array([75, 93, 66, 85, 88, 76])
scores3 = np.array([86, 76, 96, 93, 67, 87])
x = [1, 2, 3, 4, 5, 6]
plt.xticks(x, courses)
plt.plot(x, scores1)
plt.plot(x, scores2)
plt.plot(x, scores3)
plt.legend(['高二1班', '高二2班', '高二3班'], loc=(0.94, 0.80), labelspacing=0.1)
plt.title("成绩评估")
plt.show()
def function3():
courses = np.array(['语文', '数学', '英语', '物理', '化学', '生物'])
scores1 = np.array([95, 96, 85, 63, 91, 86])
scores2 = np.array([75, 93, 66, 85, 88, 76])
scores3 = np.array([86, 76, 96, 93, 67, 87])
x = [1, 2, 3, 4, 5, 6]
plt.xticks(x, courses)
plt.scatter(x, scores1)
plt.scatter(x, scores2)
plt.scatter(x, scores3)
plt.legend(['高二1班', '高二2班', '高二3班'], loc=(0.94, 0.80), labelspacing=0.1)
plt.title("成绩评估")
plt.show()
def function4():
courses = np.array(['语文', '数学', '英语', '物理', '化学', '生物'])
scores1 = np.array([95, 96, 85, 63, 91, 86])
scores2 = np.array([75, 93, 66, 85, 88, 76])
scores3 = np.array([86, 76, 96, 93, 67, 87])
x = np.linspace(0, 6, 6)
plt.xticks(x, courses)
width = 0.15
plt.bar(x - width, scores1, width=0.15)
plt.bar(x, scores2, width=0.15)
plt.bar(x + width, scores3, width=0.15)
plt.legend(['高二1班', '高二2班', '高二3班'], loc=(0.94, 0.80), labelspacing=0.1)
plt.title("成绩评估")
plt.show()
win = tk.Tk()
win.title("图表")
win.geometry("300x85")
b01=Button(win, text="雷达图", command=function1).place(x=0, y=5, width=75, height=75)
b02 = Button(win, text="折线图",command=function2).place(x=75,y=5,width=75,height=75)
b03 = Button(win, text="散点图",command=function3).place(x=150,y=5,width=75,height=75)
b04 = Button(win, text="柱状图",command=function4).place(x=225,y=5,width=75,height=75)
win.mainloop()
在刚编写完可视化部分的代码后,运行时我发现四个图表是按照固定顺序依次显示的。于是我在可视化部分的代码的基础上,又添加了一个tkinter的页面,点击对应按钮,就可以打开对应的可视化图表,且关闭图表之后还可以再打开,使程序功能更加灵活。