Matplotlib のアンカー付きオブジェクト

PythonPythonBeginner
今すぐ練習

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

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

はじめに

この実験では、Matplotlib でアンカー付きオブジェクトを使用する方法を学びます。アンカー付きオブジェクトは、プロットに補助オブジェクトを追加するために使用されます。これらのオブジェクトは、注釈、目盛りバー、凡例をプロットに追加するために使用できます。

VM のヒント

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

Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。

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

ライブラリのインポート

最初のステップは、必要なライブラリをインポートすることです。この実験では Matplotlib を使用します。

from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.offsetbox import (AnchoredOffsetbox, AuxTransformBox,
                                  DrawingArea, TextArea, VPacker)
from matplotlib.patches import Circle, Ellipse

グラフの作成

次のステップは、グラフを作成することです。単一のサブプロットを持つ簡単なグラフを作成します。

fig, ax = plt.subplots()
ax.set_aspect(1)

アンカー付きのテキストを追加する

このステップでは、グラフの左上隅に固定されたテキストボックスを追加します。

def draw_text(ax):
    """Draw a text-box anchored to the upper-left corner of the figure."""
    box = AnchoredOffsetbox(child=TextArea("Figure 1a"),
                            loc="upper left", frameon=True)
    box.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
    ax.add_artist(box)

draw_text(ax)

アンカー付きの円を追加する

このステップでは、アンカー付きオブジェクトを使用してグラフに2つの円を追加します。

def draw_circles(ax):
    """Draw circles in axes coordinates."""
    area = DrawingArea(width=40, height=20)
    area.add_artist(Circle((10, 10), 10, fc="tab:blue"))
    area.add_artist(Circle((30, 10), 5, fc="tab:red"))
    box = AnchoredOffsetbox(
        child=area, loc="upper right", pad=0, frameon=False)
    ax.add_artist(box)

draw_circles(ax)

アンカー付きの楕円を追加する

このステップでは、アンカー付きオブジェクトを使用してグラフに楕円を追加します。

def draw_ellipse(ax):
    """Draw an ellipse of width=0.1, height=0.15 in data coordinates."""
    aux_tr_box = AuxTransformBox(ax.transData)
    aux_tr_box.add_artist(Ellipse((0, 0), width=0.1, height=0.15))
    box = AnchoredOffsetbox(child=aux_tr_box, loc="lower left", frameon=True)
    ax.add_artist(box)

draw_ellipse(ax)

サイズバーを追加する

このステップでは、アンカー付きオブジェクトを使用してグラフにサイズバーを追加します。

def draw_sizebar(ax):
    """
    Draw a horizontal bar with length of 0.1 in data coordinates,
    with a fixed label center-aligned underneath.
    """
    size = 0.1
    text = r"1$^{\prime}$"
    sizebar = AuxTransformBox(ax.transData)
    sizebar.add_artist(Line2D([0, size], [0, 0], color="black"))
    text = TextArea(text)
    packer = VPacker(
        children=[sizebar, text], align="center", sep=5)  ## separation in points.
    ax.add_artist(AnchoredOffsetbox(
        child=packer, loc="lower center", frameon=False,
        pad=0.1, borderpad=0.5))  ## paddings relative to the legend fontsize.

draw_sizebar(ax)

グラフを表示する

最後のステップは、グラフを表示することです。

plt.show()

まとめ

この実験では、Matplotlibにおけるアンカー付きオブジェクトの使い方を学びました。アンカー付きオブジェクトを使って、グラフにテキスト、円、楕円、およびサイズバーを追加する方法を学びました。アンカー付きオブジェクトは、グラフに注釈や凡例を追加するために使用できる強力なツールです。