您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在Python中使用matplotlib實(shí)現(xiàn)一個(gè)交換式圖形顯示功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
一 代碼
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): #隨機(jī)選擇一個(gè)頻率,同時(shí)設(shè)置單選鈕的選中項(xiàng) 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]) #隨機(jī)選擇一個(gè)顏色,同時(shí)設(shè)置單選鈕的選中項(xiàng) c = choice(colors) radio2.set_active(colors.index(c)) l.set_color(c) #隨機(jī)選擇一個(gè)線型,同時(shí)設(shè)置單選鈕的選中項(xiàng) 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()
二 運(yùn)行結(jié)果
關(guān)于怎么在Python中使用matplotlib實(shí)現(xiàn)一個(gè)交換式圖形顯示功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。