python 实现 画图器_按钮实现Python--绘图工具matplotlib的使用

文章结束给大家来个程序员笑话:[M]

1、下载并安装合适自己Python的numpy安装包

2、下载并安装matplotlib

3、点击按钮实现画一个正弦波

from Tkinter import *

import matplotlib.pyplot as plt

import numpy as np

def draw_sin():

#draw a circle of sin

t = np.arange(1,256,1)

y = np.sin(2*np.pi*t/256)

plt.plot(t,y,'g')

plt.show()

root = Tk(className = 'DrawSin')

label = Label(root)

label['text'] = 'Draw Sin'

label.pack()

button = Button(root)

button['text'] = 'Draw'

button['command'] = draw_sin

button.pack()

root.mainloop()

效果

表现主菜单

20130516151939840

点击按钮实现绘图

20130516151948386

4、点击按钮实现波形的变更

#!usr/bin/env/python

#coding=utf-8

from Tkinter import *

import matplotlib.pyplot as plt

import numpy as np

import sys

#number of point

Number = 1024

#init frequency value

frequency = 1

#set the recur depth

sys.setrecursionlimit(1000000)

def draw_sin():

'''raw a circle of sin'''

#generate the time base

t = np.arange(1,Number,1)

#generate the signal

y = np.sin(2*np.pi*frequency*t/Number)

plt.plot(t,y)

plt.grid(True)

plt.text(900,0.75,'Frequency is '+str(frequency))

plt.show()

def frequency_plus():

'''function of add the frequency and plot the signal'''

#notice:frequency is a global variable

global frequency

frequency = frequency + 1

#clear a figure window

plt.clf()

draw_sin()

def my_button(root,label_text,button_text,button_func):

'''function of creat label and button'''

#label details

label = Label(root)

label['text'] = label_text

label.pack()

#label details

button = Button(root)

button['text'] = button_text

button['command'] = button_func

button.pack()

def main():

'''main function'''

root = Tk(className = 'DrawSin')

#draw button function

my_button(root,'Draw sin','click to Draw',draw_sin)

#frequency plus function

my_button(root,'Freq Plus','click to Plus',frequency_plus)

root.mainloop()

if __name__ == "__main__":

main()

每日一道理

生活中受伤难免,失败跌倒并不可怕,可怕的是因此而一蹶不振,失去了对人生的追求与远大的理想。没有一个人的前进道路是平平稳稳的,就算是河中穿梭航行的船只也难免颠簸,生活中所遇上的坎坷磨难不是偶尔给予的为难,而是必然所经受的磨练。

效果

表现主菜单

20130516202129338

点击按钮实现频率改变

20130516202136260

5、可能出现的问题

RuntimeError: maximum recursion depth exceeded

解决方法:

网上学习资料:

matplotlib手册

python使用matplotlib绘图

文章结束给大家分享下程序员的一些笑话语录:

很多所谓的牛人也不过如此,离开了你,微软还是微软,Google还是Google,苹果还是苹果,暴雪还是暴雪,而这些牛人离开了公司,自己什么都不是。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值