Matplotlib のプロットにカラーバーを追加する

PythonPythonBeginner
今すぐ練習

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

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

はじめに

Matplotlibは、データ可視化に使用されるPythonライブラリです。この実験では、Matplotlibのプロットにカラーバーを追加する方法を学びます。カラーバーは、カラーマップが表す値の範囲を示すのに役立ちます。

VMのヒント

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("Heatmaps") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} matplotlib/figures_axes -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} matplotlib/heatmaps -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} python/lists -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} python/tuples -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} python/importing_modules -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} python/data_visualization -.-> lab-48669{{"Matplotlib のプロットにカラーバーを追加する"}} end

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

必要なライブラリをインポートして始めましょう。Matplotlibのpyplotモジュールを使用します。このモジュールは、プロットを作成するためのインターフェイスを提供します。

import matplotlib.pyplot as plt

プロットを作成する

次に、Matplotlibのimshow関数を使ってプロットを作成します。この関数は、プロット上に画像を表示します。また、2つのサブプロット付きのグラフを作成します。

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.subplots_adjust(wspace=0.5)

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

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

プロットにカラーバーを追加する

次に、Matplotlibのmake_axes_locatable関数を使って、各サブプロットにカラーバーを追加します。この関数は既存の軸を受け取り、新しいAxesDividerに追加してAxesDividerを返します。その後、AxesDividerappend_axesメソッドを使って、元の軸の特定の側(「上」、「右」、「下」、または「左」)に新しい軸を作成できます。

ax1_divider = make_axes_locatable(ax1)
cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
cb1 = fig.colorbar(im1, cax=cax1)

ax2_divider = make_axes_locatable(ax2)
cax2 = ax2_divider.append_axes("top", size="7%", pad="2%")
cb2 = fig.colorbar(im2, cax=cax2, orientation="horizontal")
cax2.xaxis.set_ticks_position("top")

プロットを表示する

最後に、Matplotlibのshow関数を使ってプロットを表示します。

plt.show()

まとめ

この実験では、Matplotlibを使ってプロットにカラーバーを追加する方法を学びました。make_axes_locatable関数を使ってプロットに追加の軸を追加し、colorbar関数を使ってカラーバーを作成しました。また、カラーバーの向きや位置を変更する方法も学びました。