Matplotlib を使って画像グリッドを作成する

PythonPythonBeginner
今すぐ練習

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

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

はじめに

このチュートリアルでは、MatplotlibのImageGridを使って画像のグリッドを作成する方法を示します。2x2の画像グリッドを作成し、グリッドにカラーバーを追加するさまざまな方法を探ります。

VMのヒント

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

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

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

必要なライブラリとデータをインポートする

まず、グリッドを作成するために必要なライブラリとデータをインポートする必要があります。グリッドを作成するために、描画にはmatplotlib.pyplotを、サンプルデータセットを取得するためにcbookを、そしてグリッドを作成するためにImageGridを使用します。

import matplotlib.pyplot as plt
from matplotlib import cbook
from mpl_toolkits.axes_grid1 import ImageGrid

## Get sample data
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")  ## 15x15 array
extent = (-3, 4, -4, 3)

単一のカラーバー付きの2x2の画像のグリッドを作成する

最初のグリッドは、単一のカラーバー付きの2x2の画像のグリッドになります。ImageGrid関数を使ってグリッドを作成し、必要な行数と列数を指定します。また、カラーバーの位置を指定し、share_allTrueに設定してすべての画像でカラーバーを共有します。

## Create a grid of 2x2 images with a single colorbar
grid = ImageGrid(
    fig,  ## Figure object
    141,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=0.0,  ## Padding between axes
    label_mode="L",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="top",  ## Location of colorbar
    cbar_mode="single"  ## Colorbar mode
)

## Plot images on grid
for ax in grid:
    im = ax.imshow(Z, extent=extent)

## Add colorbar to grid
grid.cbar_axes[0].colorbar(im)
for cax in grid.cbar_axes:
    cax.tick_params(labeltop=False)

各画像に独自のカラーバーを持つ2x2の画像のグリッドを作成する

次のグリッドは、各画像に独自のカラーバーを持つ2x2の画像のグリッドになります。再びImageGrid関数を使いますが、今回は各画像に独自のカラーバーを持つようにするためにcbar_mode"each"に設定します。

## Create a grid of 2x2 images with each image having its own colorbar
grid = ImageGrid(
    fig,  ## Figure object
    142,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=0.1,  ## Padding between axes
    label_mode="1",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="top",  ## Location of colorbar
    cbar_mode="each",  ## Colorbar mode
    cbar_size="7%",  ## Size of colorbar
    cbar_pad="2%"  ## Padding between colorbar and images
)

## Plot images on grid and add colorbars
for ax, cax in zip(grid, grid.cbar_axes):
    im = ax.imshow(Z, extent=extent)
    cax.colorbar(im)
    cax.tick_params(labeltop=False)

各画像に独自のカラーバーと異なるカラーバー範囲を持つ2x2の画像のグリッドを作成する

最後のグリッドも、各画像に独自のカラーバーを持つ2x2の画像のグリッドになりますが、今回は各画像に異なるカラーバー範囲を使用します。各画像を描画する際に、vminvmaxを使用してカラーバー範囲を設定します。

## Create a grid of 2x2 images with each image having its own colorbar and a different colorbar range
grid = ImageGrid(
    fig,  ## Figure object
    143,  ## Location of subplot
    nrows_ncols=(2, 2),  ## Number of rows and columns
    axes_pad=(0.45, 0.15),  ## Padding between axes
    label_mode="1",  ## Label mode
    share_all=True,  ## Share colorbar across all images
    cbar_location="right",  ## Location of colorbar
    cbar_mode="each",  ## Colorbar mode
    cbar_size="7%",  ## Size of colorbar
    cbar_pad="2%"  ## Padding between colorbar and images
)

## Plot images on grid and add colorbars
limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1))  ## Different colorbar ranges
for ax, cax, vlim in zip(grid, grid.cbar_axes, limits):
    im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1])
    cb = cax.colorbar(im)
    cb.set_ticks((vlim[0], vlim[1]))

まとめ

このチュートリアルでは、MatplotlibのImageGridを使って画像のグリッドを作成する方法を学びました。グリッドにカラーバーを追加するさまざまな方法を検討しました。すべての画像に単一のカラーバーを使用する方法、各画像に独自のカラーバーを与える方法、および各画像に異なるカラーバー範囲を持つ独自のカラーバーを与える方法です。