2017-05-11 20 views
0

は、のカーソルの横にあるテキストをmatplotlibによって生成されたプロット内を移動するときに表示します。matplotlib:動的にテキスト位置を変更する

私はマウスモーションイベントを取得する方法を知っているが、どのように私は動的にテキスト要素の位置を変更のですか?

次のコードでは、カーソルの位置に応じてラインを配置する方法を示しています。

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111) 
line = ax.plot([0, 1], [0,1])[0] 

def on_mouse_move(event): 
    if None not in (event.xdata, event.ydata): 
     # draws a line from the center (0, 0) to the current cursor position 
     line.set_data([0, event.xdata], [0, event.ydata]) 
     fig.canvas.draw() 

fig.canvas.mpl_connect('motion_notify_event', on_mouse_move) 
plt.show() 

は、どのように私は、テキストと似た何かを行うことができますか?私はtext.set_dataを試しましたが、うまくいきませんでした。

+0

はhttp://stackoverflow.com/questions/42446307/update-annotations-in-matplotlib-using-buttonsを参照してください。 – ImportanceOfBeingErnest

答えて

1

いくつかの試行錯誤の末、私は解決策がtext.set_position((x, y))のように簡単であることを発見しました。

の下に次の例を参照してください。

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111) 
text = ax.text(1, 1, 'Text') 

def on_mouse_move(event): 
    if None not in (event.xdata, event.ydata): 
     text.set_position((event.xdata, event.ydata)) 
     fig.canvas.draw() 

fig.canvas.mpl_connect('motion_notify_event', on_mouse_move) 
plt.show() 
関連する問題