Matplotlib の目盛りロケータ

Beginner

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

はじめに

このチュートリアルでは、matplotlib のプロットでメモリ位置を定義する方法を学びます。メモリ位置は、x 軸と y 軸のメモリ位置を定義することで、プロットをより読みやすくするのに役立ちます。ここでは、さまざまな種類のメモリ位置と、matplotlib のプロットでそれらを実装する方法について説明します。

VM のヒント

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

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

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

ライブラリのインポート

最初のステップは、必要なライブラリをインポートすることです。ここでは、matplotlib.pyplotmatplotlib.ticker を使用します。

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

プロットの設定

次に、グラフを作成してサブプロットの配列を作成することでプロットを設定します。また、この例で軸の共通パラメータを設定する setup 関数も定義します。

fig, axs = plt.subplots(8, 1, figsize=(8, 6))

def setup(ax, title):
    """Set up common parameters for the Axes in the example."""
    ## only show the bottom spine
    ax.yaxis.set_major_locator(ticker.NullLocator())
    ax.spines[['left', 'right', 'top']].set_visible(False)

    ax.xaxis.set_ticks_position('bottom')
    ax.tick_params(which='major', width=1.00, length=5)
    ax.tick_params(which='minor', width=0.75, length=2.5)
    ax.set_xlim(0, 5)
    ax.set_ylim(0, 1)
    ax.text(0.0, 0.2, title, transform=ax.transAxes,
            fontsize=14, fontname='Monospace', color='tab:blue')

ヌルロケータの定義

ヌルロケータは、軸にメモリを配置しないロケータです。ticker.NullLocator() を使用してヌルロケータを定義できます。

setup(axs[0], title="NullLocator()")
axs[0].xaxis.set_major_locator(ticker.NullLocator())
axs[0].xaxis.set_minor_locator(ticker.NullLocator())

複数ロケータの定義

複数ロケータは、一定の間隔でメモリを配置するロケータです。ticker.MultipleLocator() を使用して複数ロケータを定義できます。

setup(axs[1], title="MultipleLocator(0.5, offset=0.2)")
axs[1].xaxis.set_major_locator(ticker.MultipleLocator(0.5, offset=0.2))
axs[1].xaxis.set_minor_locator(ticker.MultipleLocator(0.1))

固定ロケータの定義

固定ロケータは、固定された位置にメモリを配置するロケータです。ticker.FixedLocator() を使用して固定ロケータを定義できます。

setup(axs[2], title="FixedLocator([0, 1, 5])")
axs[2].xaxis.set_major_locator(ticker.FixedLocator([0, 1, 5]))
axs[2].xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4)))

線形ロケータの定義

線形ロケータは、線形スケール上で一定の間隔でメモリを配置するロケータです。ticker.LinearLocator() を使用して線形ロケータを定義できます。

setup(axs[3], title="LinearLocator(numticks=3)")
axs[3].xaxis.set_major_locator(ticker.LinearLocator(3))
axs[3].xaxis.set_minor_locator(ticker.LinearLocator(31))

インデックスロケータの定義

インデックスロケータは、インデックススケール上で一定の間隔でメモリを配置するロケータです。ticker.IndexLocator() を使用してインデックスロケータを定義できます。

setup(axs[4], title="IndexLocator(base=0.5, offset=0.25)")
axs[4].plot([0]*5, color='white')
axs[4].xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25))

自動ロケータの定義

自動ロケータは、一定の間隔で自動的にメモリを配置するロケータです。ticker.AutoLocator() を使用して自動ロケータを定義できます。

setup(axs[5], title="AutoLocator()")
axs[5].xaxis.set_major_locator(ticker.AutoLocator())
axs[5].xaxis.set_minor_locator(ticker.AutoMinorLocator())

MaxN ロケータの定義

MaxN ロケータは、軸上に最大数の目盛りを配置するロケータです。ticker.MaxNLocator() を使用して MaxN ロケータを定義できます。

setup(axs[6], title="MaxNLocator(n=4)")
axs[6].xaxis.set_major_locator(ticker.MaxNLocator(4))
axs[6].xaxis.set_minor_locator(ticker.MaxNLocator(40))

対数ロケータの定義

対数ロケータは、対数スケール上で一定の間隔で目盛りを配置するロケータです。ticker.LogLocator() を使用して対数ロケータを定義できます。

setup(axs[7], title="LogLocator(base=10, numticks=15)")
axs[7].set_xlim(10**3, 10**10)
axs[7].set_xscale('log')
axs[7].xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15))

グラフの表示

最後に、plt.show() を使用してグラフを表示できます。

plt.tight_layout()
plt.show()

まとめ

このチュートリアルでは、matplotlib のグラフで目盛りの位置を定義する方法を学びました。異なる種類の目盛りロケータと、matplotlib のグラフでそれらを実装する方法について説明しました。これにより、グラフをより読みやすく情報豊かにすることができます。