Python使用matplotlib實現(xiàn)交換式圖形顯示功能示例
本文實例講述了Python使用matplotlib實現(xiàn)交換式圖形顯示功能。分享給大家供大家參考,具體如下:
一 代碼
from random import choice
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons,Button
t = np.arange(0.0,2.0,0.01)
s0 = np.sin(2*np.pi*t)
s1 = np.sin(4*np.pi*t)
s2 = np.sin(8*np.pi*t)
fig, ax = plt.subplots()
l,= ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
#定義允許的幾種頻率,并創(chuàng)建單選鈕組件
#其中[0.05, 0.7, 0.15, 0.15]表示組件在窗口上的歸一化位置
axcolor ='lightgoldenrodyellow'
rax = plt.axes([0.05,0.7,0.15,0.15], axisbg=axcolor)
radio =RadioButtons(rax,('2 Hz','4 Hz','8 Hz'))
hzdict ={'2 Hz': s0,'4 Hz': s1,'8 Hz': s2}
def hzfunc(label):
ydata = hzdict[label]
l.set_ydata(ydata)
plt.draw()
radio.on_clicked(hzfunc)
#定義允許的幾種顏色,并創(chuàng)建單選鈕組件
rax = plt.axes([0.05,0.4,0.15,0.15], axisbg=axcolor)
colors =('red','blue','green')
radio2 =RadioButtons(rax, colors)
def colorfunc(label):
l.set_color(label)
plt.draw()
radio2.on_clicked(colorfunc)
#定義允許的幾種線型,并創(chuàng)建單選鈕組件
rax = plt.axes([0.05,0.1,0.15,0.15], axisbg=axcolor)
styles =('-','--','-.','steps',':')
radio3 =RadioButtons(rax, styles)
def stylefunc(label):
l.set_linestyle(label)
plt.draw()
radio3.on_clicked(stylefunc)
#定義按鈕單擊事件處理函數(shù),并在窗口上創(chuàng)建按鈕
def randomFig(event):
#隨機選擇一個頻率,同時設(shè)置單選鈕的選中項
hz = choice(tuple(hzdict.keys()))
hzLabels =[label.get_text()for label in radio.labels]
radio.set_active(hzLabels.index(hz))
l.set_ydata(hzdict[hz])
#隨機選擇一個顏色,同時設(shè)置單選鈕的選中項
c = choice(colors)
radio2.set_active(colors.index(c))
l.set_color(c)
#隨機選擇一個線型,同時設(shè)置單選鈕的選中項
style = choice(styles)
radio3.set_active(styles.index(style))
l.set_linestyle(style)
#根據(jù)設(shè)置的屬性繪制圖形
plt.draw()
axRnd = plt.axes([0.5,0.015,0.2,0.045])
buttonRnd =Button(axRnd,'Random Figure')
buttonRnd.on_clicked(randomFig)
#顯示圖形
plt.show()
二 運行結(jié)果

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 解決python中用matplotlib畫多幅圖時出現(xiàn)圖形部分重疊的問題
- Python3使用Matplotlib 繪制精美的數(shù)學函數(shù)圖形
- Python使用matplotlib繪制三維圖形示例
- Python使用matplotlib繪制多個圖形單獨顯示的方法示例
- Python基于matplotlib實現(xiàn)繪制三維圖形功能示例
- Python實現(xiàn)在tkinter中使用matplotlib繪制圖形的方法示例
- Python使用matplotlib實現(xiàn)繪制自定義圖形功能示例
- Python使用matplotlib填充圖形指定區(qū)域代碼示例
- Python matplotlib繪制圖形實例(包括點,曲線,注釋和箭頭)
相關(guān)文章
使用Python 操作 xmind 繪制思維導(dǎo)圖的詳細方法
在平時的工作中當我們要總結(jié)一些知識的時候就需要一款工具來畫畫流程圖,這里推薦 XMind 軟件,用 Xmind 繪制的思維導(dǎo)圖看起來思路清晰,那么今天的文章介紹關(guān)于思維導(dǎo)圖的相關(guān)知識以及用 Python 如何操作 Xmind 繪制思維導(dǎo)圖2021-10-10
Python利用QQ郵箱發(fā)送郵件的實現(xiàn)方法(分享)
下面小編就為大家?guī)硪黄狿ython利用QQ郵箱發(fā)送郵件的實現(xiàn)方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Python實現(xiàn)html轉(zhuǎn)換為pdf報告(生成pdf報告)功能示例
這篇文章主要介紹了Python實現(xiàn)html轉(zhuǎn)換為pdf報告功能,結(jié)合實例形式分析了Python使用pdfkit實現(xiàn)HTML轉(zhuǎn)換為PDF的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-05-05

