【原】使用Tkinter绘制GUI并结合Matplotlib实现交互式绘图
2014-10-16 21:01
961 查看
在数据分析的过程中,往往需要对所建立的模型进行可视化,并调整其中的某些参数。
通常情况下,在Python中可以通过Matplotlib来进行绘制图像。然而该绘制过程是静态的,也就是每次调整完参数需要重新调用绘图语句进行绘图展示。我们的目标是结合GUI组件,实现对模型参数的交互式绘图。这样,可以在展示出的GUI界面中动态的调整模型的参数,并绘制图像。
最终实现的效果如下:
可以通过GUI界面指定参数绘制散点图。
该过程需要结合Python的Tkinter库来进行GUI部分的实现。代码如下:
[/code]
通常情况下,在Python中可以通过Matplotlib来进行绘制图像。然而该绘制过程是静态的,也就是每次调整完参数需要重新调用绘图语句进行绘图展示。我们的目标是结合GUI组件,实现对模型参数的交互式绘图。这样,可以在展示出的GUI界面中动态的调整模型的参数,并绘制图像。
最终实现的效果如下:
可以通过GUI界面指定参数绘制散点图。
该过程需要结合Python的Tkinter库来进行GUI部分的实现。代码如下:
[code] #!/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
#----------------------------------------------------------------------
def drawPic():
"""
获取GUI界面设置的参数,利用该参数绘制图片
"""
#获取GUI界面上的参数
try:sampleCount=int(inputEntry.get())
except:
sampleCount=50
print '请输入整数'
inputEntry.delete(0,END)
inputEntry.insert(0,'50')
#清空图像,以使得前后两次绘制的图像不会重叠
drawPic.f.clf()
drawPic.a=drawPic.f.add_subplot(111)
#在[0,100]范围内随机生成sampleCount个数据点
x=np.random.randint(0,100,size=sampleCount)
y=np.random.randint(0,100,size=sampleCount)
color=['b','r','y','g']
#绘制这些随机点的散点图,颜色随机选取
drawPic.a.scatter(x,y,s=3,color=color[np.random.randint(len(color))])
drawPic.a.set_title('Demo: Draw N Random Dot')
drawPic.canvas.show()
if __name__ == '__main__':
matplotlib.use('TkAgg')
root=Tk()
#在Tk的GUI上放置一个画布,并用.grid()来调整布局
drawPic.f = Figure(figsize=(5,4), dpi=100)
drawPic.canvas = FigureCanvasTkAgg(drawPic.f, master=root)
drawPic.canvas.show()
drawPic.canvas.get_tk_widget().grid(row=0, columnspan=3)
#放置标签、文本框和按钮等部件,并设置文本框的默认值和按钮的事件函数
Label(root,text='请输入样本数量:').grid(row=1,column=0)
inputEntry=Entry(root)
inputEntry.grid(row=1,column=1)
inputEntry.insert(0,'50')
Button(root,text='画图',command=drawPic).grid(row=1,column=2,columnspan=3)
#启动事件循环
root.mainloop()
[/code]
相关文章推荐
- 使用Tkinter绘制GUI并结合Matplotlib实现交互式绘图
- python+matplotlib实现动态绘制图片实例代码(交互式绘图)
- Python实现在tkinter中使用matplotlib绘制图形的方法示例
- Python使用matplotlib实现基础绘图功能示例
- 远程连接Linux服务器,没有GUI的情况下使用matplotlib绘图
- 按钮实现Python--绘图工具matplotlib的使用
- 没有GUI时使用matplotlib绘图
- Python使用matplotlib实现绘制自定义图形功能示例
- tkinter的GUI设计:界面与逻辑分离(四)-- 与 matplotlib 结合
- 基于Python实现matplotlib中动态更新图片(交互式绘图)
- Centos6没有GUI的情况下使用matplotlib绘图
- 转:使用 python Matplotlib 库 绘图 及 相关问题
- python使用matplotlib绘图 -- barChart
- 使用 python Matplotlib 库绘图
- 使用 python Matplotlib 库绘图
- 使用 python Matplotlib 库绘图
- Python开发环境Wing IDE使用教程之matplotlib 2D绘图库代码调试技巧
- python使用matplotlib绘图 -- barChart
- python使用matplotlib绘图 -- barChart
- lozi混沌映射吸引子,使用python的matplotlib绘制,可以放大和缩小