Python--绘图工具matplotlib的使用


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


[python]
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() 

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()
效果

显示主菜单

 \
 


点击按钮实现画图

 \
 


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


[python]
#!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() 

#!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()
效果

显示主菜单

 \
 


点击按钮实现频率改变

 \
 


 

相关内容

    暂无相关文章

评论关闭