2017-05-03 21 views
1

Figureからポイントを選んで描画し、それらのコーディネーションを配列に格納したいと思います。それは可能ですか?はいの場合は、私を案内してくれて、助けてくれてありがとう。イベント処理matplotlip python

私はこの例を持っているが、それはラインを描いている。

from matplotlib import pyplot as plt 

class LineBuilder: 
    def __init__(self, line): 
     self.line = line 
     self.xs = list(line.get_xdata()) 
     self.ys = list(line.get_ydata()) 
     self.cid = line.figure.canvas.mpl_connect('button_press_event', self) 

    def __call__(self, event): 
     print('click', event) 
     if event.inaxes!=self.line.axes: return 
     self.xs.append(event.xdata) 
     self.ys.append(event.ydata) 
     self.line.set_data(self.xs, self.ys) 
     self.line.figure.canvas.draw() 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.set_title('click to build line segments') 
line, = ax.plot([0], [0]) # empty line 
linebuilder = LineBuilder(line) 
plt.show() 

答えて

0

まず、ラインの代わりにポイントを描画するために、あなたは、マーカーを定義し、必要に応じてラインオフをオンにする必要があります。

line, = ax.plot([0], [0], marker="o", linestyle="") 

座標はすでに配列LineBuilder.xsLineBuilder.xsに保存されているため、印刷するだけで済みます。

コンプリート例:

from matplotlib import pyplot as plt 

class LineBuilder: 
    def __init__(self, line): 
     self.line = line 
     self.xs = list(line.get_xdata()) 
     self.ys = list(line.get_ydata()) 
     self.cid = line.figure.canvas.mpl_connect('button_press_event', self) 

    def __call__(self, event): 
     if event.inaxes!=self.line.axes: return 
     self.xs.append(event.xdata) 
     self.ys.append(event.ydata) 
     self.line.set_data(self.xs, self.ys) 
     self.line.figure.canvas.draw_idle() 
     print(self.xs) 
     print(self.ys) 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.set_title('click to build line segments') 
line, = ax.plot([0], [0], marker="o", linestyle="") 
linebuilder = LineBuilder(line) 
plt.show() 
+0

は、私は:) –

+0

グレート探しているものを、この場合には、あなたが(https://meta.stackexchange.com/questions/5234/ [受け入れる]ことをお願いします答えを受け入れる - ) - その答え。 SOが一般的にどのように機能しているかを確認するには、[ツアー]を利用することもできます。 – ImportanceOfBeingErnest