AnnotationBbox を使ってグラフに注釈を付ける

Beginner

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

はじめに

この実験では、Matplotlib の AnnotationBbox を使って、テキスト、形状、画像を使って図に注釈を付ける方法を学びます。AnnotationBbox は Axes.annotate よりも細かい制御方法です。3 つの異なる OffsetBoxes:TextArea、DrawingArea、および OffsetImage を見ていきます。

VM のヒント

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

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

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

点のプロット

始めに、後で注釈を付ける 2 つの点をプロットしましょう。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

## 注釈を付ける最初の位置を定義する (マーカーで表示する)
xy1 = (0.5, 0.7)
ax.plot(xy1[0], xy1[1], ".r")

## 注釈を付ける 2 番目の位置を定義する (今回はマーカーで表示しない)
xy2 = [0.3, 0.55]

## すべてを表示するために表示範囲を固定する
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

plt.show()

TextArea で注釈を付ける

次に、TextArea を使って最初の点に注釈を付けましょう。

from matplotlib.offsetbox import AnnotationBbox, TextArea

## 1 番目の位置にテキストボックス ('Test 1') で注釈を付ける
offsetbox = TextArea("Test 1")

ab = AnnotationBbox(offsetbox, xy1,
                    xybox=(-20, 40),
                    xycoords='data',
                    boxcoords="offset points",
                    arrowprops=dict(arrowstyle="->"),
                    bboxprops=dict(boxstyle="sawtooth"))

ax.add_artist(ab)

plt.show()

DrawingArea で注釈を付ける

次に、DrawingArea を使って円形のパッチを使って 2 番目の点に注釈を付けましょう。

from matplotlib.offsetbox import DrawingArea
from matplotlib.patches import Circle

## 2 番目の位置に円形のパッチで注釈を付ける
da = DrawingArea(20, 20, 0, 0)
p = Circle((10, 10), 10)
da.add_artist(p)

ab = AnnotationBbox(da, xy2,
                    xybox=(1., xy2[1]),
                    xycoords='data',
                    boxcoords=("axes fraction", "data"),
                    box_alignment=(0.2, 0.5),
                    arrowprops=dict(arrowstyle="->"),
                    bboxprops=dict(alpha=0.5))

ax.add_artist(ab)

plt.show()

OffsetImage で注釈を付ける

最後に、グレース・ホッパーの画像を使って OffsetImage を使って 2 番目の点に注釈を付けましょう。

from matplotlib.cbook import get_sample_data
from matplotlib.offsetbox import OffsetImage

## 2 番目の位置に画像 (生成されたピクセル配列) で注釈を付ける
arr = np.arange(100).reshape((10, 10))
im = OffsetImage(arr, zoom=2)
im.image.axes = ax

ab = AnnotationBbox(im, xy2,
                    xybox=(-50., 50.),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.3,
                    arrowprops=dict(arrowstyle="->"))

ax.add_artist(ab)

## 2 番目の位置に別の画像 (グレース・ホッパーの肖像画) で注釈を付ける
with get_sample_data("grace_hopper.jpg") as file:
    arr_img = plt.imread(file)

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax

ab = AnnotationBbox(imagebox, xy2,
                    xybox=(120., -80.),
                    xycoords='data',
                    boxcoords="offset points",
                    pad=0.5,
                    arrowprops=dict(
                        arrowstyle="->",
                        connectionstyle="angle,angleA=0,angleB=90,rad=3")
                    )

ax.add_artist(ab)

plt.show()

まとめ

この実験では、Matplotlib の AnnotationBbox を使って、テキスト、形状、画像を使ってグラフに注釈を付ける方法を学びました。TextArea、DrawingArea、OffsetImage の 3 つの異なる OffsetBox を見てきました。AnnotationBbox を使うことで、Axes.annotate を使う場合よりも注釈に対してより細かい制御ができます。