Matplotlib を使った独自のグリッド軸の作成

PythonPythonBeginner
オンラインで実践に進む

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

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

はじめに

この実験では、mpl_toolkits.axes_grid1 モジュールを使って Matplotlib で独自の軸のグリッドを作成する方法を学びます。2 つの例を作成します。1 つは固定された軸のサイズとパディングを持つもので、もう 1 つは拡大縮小可能な軸のサイズと固定されたパディングを持つものです。

VM のヒント

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

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

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

必要なライブラリのインポート

まず、必要なライブラリをインポートします。可視化には matplotlib.pyplot を、独自の軸のグリッドを作成するためには mpl_toolkits.axes_grid1 を使用します。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider, Size

ヘルパー関数の定義

軸の中央にラベルを配置し、軸の目盛りを削除するためのヘルパー関数 label_axes() を定義します。

def label_axes(ax, text):
    """Place a label at the center of an Axes, and remove the axis ticks."""
    ax.text(.5,.5, text, transform=ax.transAxes,
            horizontalalignment="center", verticalalignment="center")
    ax.tick_params(bottom=False, labelbottom=False,
                   left=False, labelleft=False)

固定サイズとパディングを持つ独自の軸のグリッドの作成

固定サイズとパディングを持つ独自の軸のグリッドを作成します。Divider クラスを使って、軸の矩形を horiz * vert で指定されたサイズのグリッドに分割します。その後、add_axes() メソッドを使って図に 4 つの軸を追加し、Divider クラスの new_locator() メソッドを使って各軸の位置を指定します。

## Sizes are in inches.
horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)]
vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Fixed axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

拡大縮小可能なサイズと固定パディングを持つ独自の軸のグリッドの作成

拡大縮小可能なサイズと固定パディングを持つもう 1 つの独自の軸のグリッドを作成します。図のサイズに合わせて拡大縮小する軸のサイズを指定するために、Size.Scaled() オプションを使用します。残りの手順は前の例と同様です。

## Sizes are in inches.
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)]
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Scalable axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

まとめ

この実験では、mpl_toolkits.axes_grid1 モジュールを使って Matplotlib で独自の軸のグリッドを作成する方法を学びました。2 つの例を作成しました。1 つは固定された軸のサイズとパディングを持つもので、もう 1 つは拡大縮小可能な軸のサイズと固定されたパディングを持つものです。Divider クラスを使って、軸の矩形を horiz * vert で指定されたサイズのグリッドに分割し、Divider クラスの add_axes() メソッドと new_locator() メソッドを使って図に軸を追加しました。