2013-11-26 53 views
0

在Matplotlib中,我想單擊並標記點(實際上,最終從它的位置計算出一個點),但它似乎不可能。即使是一些簡單的像這不起作用:爲什麼我不能繪製選定的點?

from pylab import * 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(np.random.rand(10)) 

def onclick(event): 
    print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
     event.button, event.x, event.y, event.xdata, event.ydata) 
    plt.plot(event.xdata,event.ydata,',') 

cid = fig.canvas.mpl_connect('button_press_event', onclick) 
show() 
+0

你得到一個錯誤? – tacaswell

+0

不,它只是不顯示 – NoBugs

回答

1

','是單個像素的標記,儘量'o'或更大的東西來代替。

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(np.random.rand(10)) 

def onclick(event): 
    print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
     event.button, event.x, event.y, event.xdata, event.ydata) 
    ax.plot(event.xdata,event.ydata,'o') 
    plt.draw_if_interactive() 

cid = fig.canvas.mpl_connect('button_press_event', onclick) 
show() 

enter image description here

+0

我以前用','作爲標記。與o,沒有變化,仍然不起作用。 – NoBugs

+0

我發佈的代碼在當前主控上正常工作。你使用的是什麼版本的mpl? – tacaswell

+0

@NoBugs另外,後端是什麼?交互性不適用於ipython筆記本 – tacaswell

相關問題