Matplotlib のカラーバー付きインセットエイクス

PythonPythonBeginner
今すぐ練習

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

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

はじめに

このチュートリアルでは、mpl_toolkits.axes_grid1.inset_locator.inset_axes を使ってカラーバーの位置、高さ、幅を制御する方法を紹介します。これは、プロットにカラーバーを追加したいが、そのサイズと位置をカスタマイズしたい場合に便利です。

VMのヒント

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/comments("Comments") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("Heatmaps") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/using_packages("Using Packages") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} matplotlib/figures_axes -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/comments -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} matplotlib/heatmaps -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/lists -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/tuples -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/importing_modules -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/using_packages -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} python/data_visualization -.-> lab-48670{{"Matplotlib のカラーバー付きインセットエイクス"}} end

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

まず、必要なライブラリをインポートする必要があります。matplotlibと、mpl_toolkits.axes_grid1からのinset_axesです。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

プロットと画像を作成する

次に、インセット エイクスを使ってカラーバーを追加する方法を示すために、プロットと画像を作成します。

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])

im1 = ax1.imshow([[1, 2], [2, 3]])
im2 = ax2.imshow([[1, 2], [2, 3]])

インセット エイクスを使ってカラーバーを追加する

次に、インセット エイクスを使って各画像にカラーバーを追加します。最初のカラーバーはax1に、2番目のカラーバーはax2に追加されます。

## add colorbar to ax1
axins1 = inset_axes(
    ax1,
    width="50%",  ## width: 50% of parent_bbox width
    height="5%",  ## height: 5%
    loc="upper right",
)
axins1.xaxis.set_ticks_position("bottom")
fig.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])

## add colorbar to ax2
axins2 = inset_axes(
    ax2,
    width="5%",  ## width: 5% of parent_bbox width
    height="50%",  ## height: 50%
    loc="lower left",
    bbox_to_anchor=(1.05, 0., 1, 1),
    bbox_transform=ax2.transAxes,
    borderpad=0,
)
fig.colorbar(im2, cax=axins2, ticks=[1, 2, 3])

プロットを表示する

最後に、plt.show() を使ってプロットを表示します。

plt.show()

まとめ

このチュートリアルでは、カスタマイズされたサイズと位置でカラーバーをプロットに追加するために、mpl_toolkits.axes_grid1.inset_locator.inset_axes をどのように使用するかを学びました。inset_axes 関数を使うことで、カラーバーの位置、高さ、幅を制御でき、見た目の良いプロットを作成しやすくなります。