注意点:Python第三包函数或者包经常更新,出错很可能是名字变了。。。
比如:show()变为draw(),NavigationToolbar2Tktag 变为 NavigationToolbar2Tk
使用matplotlib库绘制可视化图形,其中X=[1,2,3,4,5,6,7,8,9,10], Y=[80,50,60,48,51,95,65,85,75,78],横轴为学号,纵轴为分数。
# -*- coding: utf-8 -*- """ Spyder 编辑器 这是一个临时脚本文件。 """ # sphinx_gallery_thumbnail_number = 3 import matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = [80,50,60,48,51,95,65,85,75,78] plt.plot(x, y, label='result') # Plot some data on the (implicit) axes. plt.xlabel('x label:学号', fontproperties='SimHei', fontsize=20) plt.ylabel('y label:成绩', fontproperties='SimHei', fontsize=20) plt.title("Simple Plot:学生成绩表", fontproperties='SimHei') plt.legend()
使用matplotlib库绘制正弦波图形,并将其放入到Tkinter(或QT)窗口视窗中
# coding=utf-8 import sys import tkinter as Tk import matplotlib from numpy import arange, sin, pi from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk from matplotlib.backend_bases import key_press_handler from matplotlib.figure import Figure matplotlib.use('TkAgg') root =Tk.Tk() root.title("正弦波图形- matplotlib in TK") #设置图形尺寸与质量 f =Figure(figsize=(5,4), dpi=100) a = f.add_subplot(111) t = arange(0.0,3,0.01) s = sin(2*pi*t) #绘制图形 a.plot(t, s) #把绘制的图形显示到tkinter窗口上 canvas =FigureCanvasTkAgg(f, master=root) canvas.draw() canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #把matplotlib绘制图形的导航工具栏显示到tkinter窗口上 toolbar =NavigationToolbar2Tk(canvas, root) toolbar.update() canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) #定义并绑定键盘事件处理函数 def on_key_event(event): print('you pressed %s'% event.key) key_press_handler(event, canvas, toolbar) canvas.mpl_connect('key_press_event', on_key_event) #按钮单击事件处理函数 def _quit(): #结束事件主循环,并销毁应用程序窗口 root.quit() root.destroy() button =Tk.Button(master=root, text='Quit', command=_quit) button.pack(side=Tk.BOTTOM) Tk.mainloop()