官方说明链接地址:点我
标识位置:loc
此案例程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#!/usr/bin/env python # coding:utf-8 """ Author: Chaos --<Chaosimpler@gmail.com> Purpose: 修改Matplotlib的后端,实现在Tkinter的GUI绘制图像 Created: 2014-10-15 """ import numpy as np from tkinter import * import matplotlib from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure # 在[0,100]范围内随机生成sampleCount个数据点 x = np.arange(0.0, 1.0, 0.01) y = (1 + np.sin(2 * np.pi * x)) # ---------------------------------------------------------------------- def drawPic(): global x,y """ 获取GUI界面设置的参数,利用该参数绘制图片 """ # 获取GUI界面上的参数 try: sampleCount = float(inputEntry.get()) except: sampleCount = 1 #print( '请输入整数') '请输入整数' inputEntry.delete(0, END) inputEntry.insert(0, '1') # 清空图像,以使得前后两次绘制的图像不会重叠 drawPic.f.clf() drawPic.a = drawPic.f.add_subplot(111) # x =np.concatenate((x,[x[-1]+0.01])) x=np.append(x,x[-1]+0.01) # y = np.concatenate((y, [sampleCount])) y = np.append(y, sampleCount)#向Y数组中添加元素 # 绘制这些随机点的散点图,颜色随机选取 drawPic.a.plot(x,y,label="val",color = 'r') # leg = plt.legend(loc='best', ncol=2, mode="expand", shadow=True, fancybox=True) # leg.get_frame().set_alpha(0)#透明度 leg = drawPic.a.legend(loc='upper right' ,ncol=1, mode="None", shadow=True, fancybox=True) leg.get_frame().set_alpha(0) #drawPic.a.legend(x, y, s=3) # drawPic.a.legend() drawPic.a.set_title('value') drawPic.canvas.show() # drawPic.f.show() if __name__ == '__main__': #matplotlib.use('TkAgg') root = Tk() # 在Tk的GUI上放置一个画布,并用.grid()来调整布局 drawPic.f = Figure(figsize=(5, 4), dpi=150) drawPic.canvas = FigureCanvasTkAgg(drawPic.f, master=root) drawPic.canvas.show() drawPic.canvas.get_tk_widget().grid(row=0, columnspan=3)#第0行,占用3列 # 放置标签、文本框和按钮等部件,并设置文本框的默认值和按钮的事件函数 Label(root, text='请输入样本数量:').grid(row=1, column=0) inputEntry = Entry(root) inputEntry.grid(row=1, column=1) inputEntry.insert(0, '1') Button(root, text='画图', command=drawPic).grid(row=1, column=2, columnspan=3) # Button(root,text="增加",command=insbut).gird(row=2) # 启动事件循环 root.mainloop() |
模式:水平扩展或无
向x数组中添加元素的两种方法:
1 2 |
# x =np.concatenate((x,[x[-1]+0.01])) x=np.append(x,x[-1]+0.01) |
https://www.cnblogs.com/cymwill/p/8118135.html
转载请注明:徐自远的乱七八糟小站 » 【原创】matplotlib初学习