Matplotlib の図と軸の入力/離脱イベント

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

MatplotlibはPython用のデータ可視化ライブラリです。Pythonで静的、アニメーション、インタラクティブな可視化を作成するためのさまざまなツールを提供します。Matplotlibのインタラクティブ機能の1つは、マウスが図または軸に入ったり離れたりするときを検出する機能です。この実験では、MatplotlibのFigureとAxesの入力/出力イベントを使用して、図と軸の枠の色を変更する方法を学びます。

VMのヒント

VMの起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebookを使って練習しましょう。

時々、Jupyter Notebookが読み込み終了するまで数秒待つ必要がある場合があります。Jupyter Notebookの制限により、操作の検証を自動化することはできません。

学習中に問題がある場合は、Labbyにお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib(("Matplotlib")) -.-> matplotlib/AdvancedTopicsGroup(["Advanced Topics"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/DataStructuresGroup -.-> python/tuples("Tuples") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("Event Handling") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} matplotlib/figures_axes -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} python/tuples -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} python/function_definition -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} python/build_in_functions -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} python/importing_modules -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} matplotlib/event_handling -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} python/data_visualization -.-> lab-48730{{"Matplotlib の図と軸の入力/離脱イベント"}} end

Matplotlibのインポート

Matplotlibを使用し始める前に、インポートする必要があります。また、グラフを作成するためのシンプルなインターフェイスを提供するpyplotモジュールもインポートします。

import matplotlib.pyplot as plt

図と軸の作成

subplots関数を使って、2つのサブプロット(軸)付きの図を作成します。また、図のタイトルも設定します。

fig, axs = plt.subplots(2, 1)
fig.suptitle('Mouse Hover Over Figure or Axes to Trigger Events')

イベントハンドラの定義

ここでは、4つのイベントハンドラ関数を定義します。on_enter_axeson_leave_axeson_enter_figure、およびon_leave_figure。これらの関数は、マウスが軸または図に入ったり離れたりするときに呼び出されます。

def on_enter_axes(event):
    print('enter_axes', event.inaxes)
    event.inaxes.patch.set_facecolor('yellow')
    event.canvas.draw()

def on_leave_axes(event):
    print('leave_axes', event.inaxes)
    event.inaxes.patch.set_facecolor('white')
    event.canvas.draw()

def on_enter_figure(event):
    print('enter_figure', event.canvas.figure)
    event.canvas.figure.patch.set_facecolor('red')
    event.canvas.draw()

def on_leave_figure(event):
    print('leave_figure', event.canvas.figure)
    event.canvas.figure.patch.set_facecolor('grey')
    event.canvas.draw()

イベントハンドラを図のキャンバスに接続する

ここでは、mpl_connectメソッドを使ってイベントハンドラを図のキャンバスに接続します。これにより、マウスが図または軸に入ったり離れたりするときにイベントハンドラがトリガーされるようになります。

fig.canvas.mpl_connect('figure_enter_event', on_enter_figure)
fig.canvas.mpl_connect('figure_leave_event', on_leave_figure)
fig.canvas.mpl_connect('axes_enter_event', on_enter_axes)
fig.canvas.mpl_connect('axes_leave_event', on_leave_axes)

図の表示

ここでは、show関数を使って図を表示します。

plt.show()

まとめ

この実験では、Matplotlibの図と軸の入力/離脱イベントを使って、図と軸の枠の色を変更する方法を学びました。2つのサブプロット付きの図を作成し、図と軸への入力と離脱に対するイベントハンドラ関数を定義し、イベントハンドラを図のキャンバスに接続し、そして図を表示しました。