Matplotlib を使ったズームインインセットの作成

PythonPythonBeginner
今すぐ練習

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

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

はじめに

Matplotlibは、Pythonにおける人気のあるデータ可視化ライブラリです。これは、さまざまな種類のプロットやグラフを作成するための多くのツールを提供します。Matplotlibの便利な機能の1つは、プロットの特定の領域をズームインする機能であり、これにより、データをより詳細に分析することができます。この実験では、Matplotlibを使用してズームインしたインセットを作成する方法を学びます。

VMのヒント

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

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

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

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

ズームインしたインセットを作成する前に、必要なライブラリをインポートする必要があります。この実験では、matplotlib.pyplotnumpy を使用します。

import matplotlib.pyplot as plt
import numpy as np

グラフとサブプロットを作成する

次に、データを表示するためのグラフとサブプロットを作成します。ズームインしたインセットの2つの異なる例を示すために、横並びに2つのサブプロットを作成します。

fig, (ax, ax2) = plt.subplots(ncols=2, figsize=[6, 3])

サイズバー付きのズームインインセットを作成する

最初のサブプロットでは、サイズバー付きのズームインインセットを作成します。これは、.zoomed_inset_axes メソッドを使用してズームインインセットを作成する方法を示します。

## Set the aspect ratio of the plot to 1
ax.set_aspect(1)

## Create a zoomed inset in the upper right corner of the plot
axins = zoomed_inset_axes(ax, zoom=0.5, loc='upper right')

## Set the number of ticks on the inset axes
axins.yaxis.get_major_locator().set_params(nbins=7)
axins.xaxis.get_major_locator().set_params(nbins=7)

## Hide the tick labels on the inset axes
axins.tick_params(labelleft=False, labelbottom=False)

## Define a function to add a size bar to the plot
def add_sizebar(ax, size):
    asb = AnchoredSizeBar(ax.transData,
                          size,
                          str(size),
                          loc=8,
                          pad=0.1, borderpad=0.5, sep=5,
                          frameon=False)
    ax.add_artist(asb)

## Add a size bar to the main plot and the inset plot
add_sizebar(ax, 0.5)
add_sizebar(axins, 0.5)

インセットズームとマーク付きのインセットを持つ画像を作成する

2番目のサブプロットでは、インセットズームとマーク付きのインセットを持つ画像を作成します。これは、関心のある領域をマークしてインセット軸に接続するための.mark_insetメソッドの使い方を示します。

## Load sample data for the image
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")  ## 15x15 array
extent = (-3, 4, -4, 3)
Z2 = np.zeros((150, 150))
ny, nx = Z.shape
Z2[30:30+ny, 30:30+nx] = Z

## Display the image in the subplot
ax2.imshow(Z2, extent=extent, origin="lower")

## Create a zoomed inset in the upper left corner of the plot
axins2 = zoomed_inset_axes(ax2, zoom=6, loc=1)

## Display the image in the inset plot
axins2.imshow(Z2, extent=extent, origin="lower")

## Set the x and y limits of the inset plot to show the region of interest
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins2.set_xlim(x1, x2)
axins2.set_ylim(y1, y2)

## Set the number of ticks on the inset axes
axins2.yaxis.get_major_locator().set_params(nbins=7)
axins2.xaxis.get_major_locator().set_params(nbins=7)

## Hide the tick labels on the inset axes
axins2.tick_params(labelleft=False, labelbottom=False)

## Mark the region of interest and connect it to the inset axes
mark_inset(ax2, axins2, loc1=2, loc2=4, fc="none", ec="0.5")

グラフを表示する

最後に、plt.show() メソッドを使ってグラフを表示します。

plt.show()

まとめ

この実験では、Matplotlibを使ってズームインインセットを作成する方法を学びました。.zoomed_inset_axes.mark_inset メソッドを使って、ズームインインセットの2つの異なる例を作成しました。これらのメソッドを使うことで、データをより詳細に分析し、元のプロットでは見えない洞察を得ることができます。