初級cnn研究輔助:python的matplotlib顯示圖片 之 按鈕和觸發事件

一、點擊顯示出來的圖片,出現別的:

點擊左側圖片,顯示右側圖片,並在你點擊的位置畫點。

from matplotlib import pyplot as py
from matplotlib.widgets import Button,RadioButtons
import Image
def on_press(event):
    if event.inaxes == None:
        print "none"
        return 
    fig = event.inaxes.figure
    ax = fig.add_subplot(122)
    img_gray = Image.open("./Alex.jpg").convert("L")
    ax.imshow(img_gray, cmap="gray")
    print event.x
    ax1.scatter(event.xdata, event.ydata)
    py.axis("off")
    fig.canvas.draw()
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    fig = py.figure()
    fig.canvas.mpl_connect("button_press_event", on_press) 
    ax1 = fig.add_subplot(121)   
    ax1.imshow(img)
    py.axis("off")
    py.show()
效果:


解釋:

fig.canvas.mpl_connect("button_press_event", on_press)#在這個figure上加點擊事件,點擊後的情況在自己寫的on_press()方法裏
def on_press(event):
       event.inaxes.figure.canvas.draw()#用於圖片刷新
       event.x#事件的座標用於其他按鈕點擊和figure點擊發生衝突時判斷返回
       event.xdata,event.ydata#鼠標點擊的位置,與上面那個座標表示形式不同
二、普通按鈕

from matplotlib import pyplot as py
from matplotlib.widgets import Button,RadioButtons
import Image
def on_press(event):
    if event.inaxes == None:
        print "none"
        return 
    fig = event.inaxes.figure
    ax = fig.add_subplot(122)
    img_gray = Image.open("./Alex.jpg").convert("L")
    ax.imshow(img_gray, cmap="gray")
    print event.x
    ax1.scatter(event.xdata, event.ydata)
    py.axis("off")
    fig.canvas.draw()
def button_press(event):
    print 'button is pressed!'
def draw_button():
    global button#must global
    point = py.axes([0.3,0.03,0.1,0.03])
    button = Button(point, "click me")
    button.on_clicked(button_press)
if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    fig = py.figure()
    draw_button()
    fig.canvas.mpl_connect("button_press_event", on_press) 
    ax1 = fig.add_subplot(121)   
    ax1.imshow(img)
    py.axis("off")
    py.show()
效果:

點擊按鈕,控制檯輸出“button is pressed!”

註解:

buttonax = plt.axes([0.8,0.03,0.05,0.03])#按鈕的位置大小
button = Button(buttonax, "i'am a button")#按鈕
button.on_clicked(save)#按鈕的點擊,save()是自己定議的點擊後的事件
def save(event):
    print  'i am press'

三、RadioButton:

def button_press(event):
    print 'button is pressed!'
def radio_press(label):
    print 'select: ',label
    radiobutton.set_active(0)
def draw_button():
    global button#must global
    global radiobutton
    print 'button'
    point = py.axes([0.2,0.03,0.1,0.03])
    button = Button(point, "click me")
    button.on_clicked(button_press)
    point_two = py.axes([0.6, 0.03, 0.2, 0.05])
    radiobutton = RadioButtons(point_two, ("select me", "or me"))
    radiobutton.on_clicked(radio_press)

if __name__ == "__main__":
    img = Image.open("./Alex.jpg")
    fig = py.figure()
    draw_button()
    fig.canvas.mpl_connect("button_press_event", on_press) 
    ax1 = fig.add_subplot(121)   
    ax1.imshow(img)
    py.axis("off")
    py.show()
效果:

註解:

radiobutton.set_active(0)#"DIFFERENT"被選擇
def select(label):
      print "label"#label is "DIFFERENT" or "SAME"  

然後你可能會發現點擊右側按鈕時也會觸發右側出現圖片,這該怎麼辦呢?

在一的例子中有event.x就是幹這個用的,通過判斷這個值判斷鼠標點擊的位置。但是沒人的電腦不同,結果不同。

發佈了74 篇原創文章 · 獲贊 50 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章