ベースマッププロットにポップアップボックスを作成する方法を知りたいです。ある場所にマウスを合わせると、ポップアップボックスが表示されます。
これは可能ですか?
ベースマッププロットにポップアップボックスを作成する方法を知りたいです。ある場所にマウスを合わせると、ポップアップボックスが表示されます。
これは可能ですか?
はい、matplotlibのイベント処理フレームワークのおかげで可能です。あなたが特に興味を持っていることを実行するすでに書かれた例を見つけることができなかったので、私はそれを書きました(matplotlibソースに含めるために提案します)。
http://matplotlib.sourceforge.net/users/event_handling.htmlをよく読んで、何が起こっているのかを最もよく理解します。完璧な解決策のように聞こえますが、「pick_event」はマウスクリック用であり、マウスオーバーイベント用ではなく、この場合は機能しないことに注意してください。
私のコードは、必要に応じて非常にうまくオブジェクト化できますが、次のようになります。
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
points_with_annotation = []
for i in range(10):
point, = plt.plot(i, i, 'o', markersize=10)
annotation = ax.annotate("Mouseover point %s" % i,
xy=(i, i), xycoords='data',
xytext=(i + 1, i), textcoords='data',
horizontalalignment="left",
arrowprops=dict(arrowstyle="simple",
connectionstyle="arc3,rad=-0.2"),
bbox=dict(boxstyle="round", facecolor="w",
edgecolor="0.5", alpha=0.9)
)
# by default, disable the annotation visibility
annotation.set_visible(False)
points_with_annotation.append([point, annotation])
def on_move(event):
visibility_changed = False
for point, annotation in points_with_annotation:
should_be_visible = (point.contains(event)[0] == True)
if should_be_visible != annotation.get_visible():
visibility_changed = True
annotation.set_visible(should_be_visible)
if visibility_changed:
plt.draw()
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
うまくいけば、すべてがかなり読みやすいはずです。コードの概要は次のとおりです。