はじめに
この実験では、Matplotlib におけるピッキングの概念を紹介します。アーティストをピックする機能は、ユーザーの操作に応答するインタラクティブなビジュアライゼーションを構築するために使用できる強力なツールです。ここでは、単純なピッキング、カスタムヒットテスト関数を使ったピッキング、散布図でのピッキング、および画像のピッキングについて説明します。
VM のヒント
VM の起動が完了したら、左上隅をクリックして ノートブック タブに切り替えて、Jupyter Notebook を使って練習しましょう。
時々、Jupyter Notebook が読み込み終了するまで数秒待つ必要があります。Jupyter Notebook の制限により、操作の検証は自動化できません。
学習中に問題に直面した場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。
単純なピッキング、線、矩形、およびテキスト
アーティストの「picker」プロパティを設定することで単純なピッキングを有効にします。これにより、マウスイベントがアーティストの上にある場合、アーティストがピックイベントを発生させることができるようになります。線、矩形、およびテキストを含む単純なプロットを作成し、これらの各アーティストにピッキングを有効にします。
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.set_title('click on points, rectangles or text', picker=True)
ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
line, = ax1.plot(rand(100), 'o', picker=True, pickradius=5)
## Pick the rectangle.
ax2.bar(range(10), rand(10), picker=True)
for label in ax2.get_xticklabels(): ## Make the xtick labels pickable.
label.set_picker(True)
カスタムヒットテスト関数の作成
このステップでは、picker をコール可能な関数に設定することでカスタムピッカーを定義します。この関数は、アーティストがマウスイベントによってヒットされたかどうかを判断します。マウスイベントがアーティストの上にある場合、hit=True を返し、props は .PickEvent 属性に追加したいプロパティの辞書です。
def line_picker(line, mouseevent):
"""
Find the points within a certain distance from the mouseclick in
data coords and attach some extra attributes, pickx and picky
which are the data points that were picked.
"""
if mouseevent.xdata is None:
return False, dict()
xdata = line.get_xdata()
ydata = line.get_ydata()
maxd = 0.05
d = np.sqrt(
(xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
ind, = np.nonzero(d <= maxd)
if len(ind):
pickx = xdata[ind]
picky = ydata[ind]
props = dict(ind=ind, pickx=pickx, picky=picky)
return True, props
else:
return False, dict()
def onpick2(event):
print('onpick2 line:', event.pickx, event.picky)
fig, ax = plt.subplots()
ax.set_title('custom picker for line data')
line, = ax.plot(rand(100), rand(100), 'o', picker=line_picker)
fig.canvas.mpl_connect('pick_event', onpick2)
散布図でのピッキング
散布図は ~matplotlib.collections.PathCollection に基づいています。散布図を作成してピッキングを有効にします。
x, y, c, s = rand(4, 100)
def onpick3(event):
ind = event.ind
print('onpick3 scatter:', ind, x[ind], y[ind])
fig, ax = plt.subplots()
ax.scatter(x, y, 100*s, c, picker=True)
fig.canvas.mpl_connect('pick_event', onpick3)
画像のピッキング
.Axes.imshow を使用して描画された画像は ~matplotlib.image.AxesImage オブジェクトです。複数の画像を含む図を作成してピッキングを有効にします。
fig, ax = plt.subplots()
ax.imshow(rand(10, 5), extent=(1, 2, 1, 2), picker=True)
ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True)
ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True)
ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True)
ax.set(xlim=(0, 5), ylim=(0, 5))
def onpick4(event):
artist = event.artist
if isinstance(artist, AxesImage):
im = artist
A = im.get_array()
print('onpick4 image', A.shape)
fig.canvas.mpl_connect('pick_event', onpick4)
まとめ
この実験では、Matplotlib の様々なアーティスト(線、矩形、テキスト、散布図、画像など)に対してピッキングを有効にする方法を学びました。また、より複雑なピッキング動作を可能にするためのカスタムヒットテスト関数を定義する方法も学びました。この強力なツールを使うことで、ユーザーのアクションに応答するインタラクティブなビジュアライゼーションを作成することができます。