在主窗口的指定区域嵌入饼图,代码如下(python3.7):
import tkinterimport matplotlibfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAggimport matplotlib.pyplot as pltplt.rcParams[\'font.sans-serif\']=[\'SimHei\']#设置字体,便于显示中文root = tkinter.Tk()root.geometry(\'900x500+250+100\')root.resizable(False, False)root.title(\'把matplotlib的饼图放在tkinter上\')matplotlib.use(\'TkAgg\')pie = plt.figure(figsize=(2.2, 2.2),facecolor=\"#F0F0F0\") # 调节图形大小、背景色pie.labels = [\'正确\', \'错误\', \'待测\'] # 给出要展示的内容(标签)pie.sizes = [5, 6,7] # 每个饼块的值pie.colors = [\'green\', \'red\', \'orange\'] # 自定义颜色pie.explode = (0.05, 0.04, 0.04) # 分割,值越大分割出的间隙越大pie.patches, pie.text2, pie.text1 = plt.pie(pie.sizes,explode=pie.explode,labels=pie.labels,colors=pie.colors,autopct=\'%3.1f%%\', # 数值保留小数位数shadow=True, # 有、无阴影设置startangle=90, # 逆时针起始角度设置pctdistance=1.4,#标签数字和圆心的距离textprops={\'fontsize\': 8, \'color\': \'#000080\'})plt.axis(\'equal\') #正圆for t in pie.text2:#设置饼图标签的字体大小t.setsize = 8canvas_statis = FigureCanvasTkAgg(pie, root)canvas_statis.get_tk_widget().place(x=665, y=40)#根据坐标,放在指主窗口的指定位置root.mainloop()