大目盛りと小目盛り#

メジャー ティッカーとマイナー ティッカーの使用方法を示します。

関連する 2 つのクラスはLocators とFormatters です。ロケーターは目盛りの位置を決定し、フォーマッターは目盛りラベルの書式設定を制御します。

小目盛はデフォルトでオフになっています (NullLocatorとを使用NullFormatter)。マイナーロケーターを設定することで、ラベルなしでマイナーティックをオンにすることができます。マイナー フォーマッタを設定することで、マイナー ティック ラベルをオンにすることができます。

MultipleLocator基数の倍数にティックを配置します。 目盛りラベルをフォーマットするためにStrMethodFormatterフォーマット文字列 (例えば、'{x:d}'または'{x:1.2f}' または) を使用します (フォーマット文字列の変数は でなければなりません)。a の場合、文字列はor に直接渡すことができます。適切なが作成され、自動的に使用されます。'{x:1.1f} cm''x'StrMethodFormatterAxis.set_major_formatterAxis.set_minor_formatterStrMethodFormatter

pyplot.gridy 軸と y 軸の大目盛りのグリッド設定を一緒に変更します。特定の軸の副目盛りのグリッドを制御する場合は、次のように使用します。

ax.xaxis.grid(True, which='minor')

特定のロケーターまたはフォーマッター インスタンスは、単一の軸でのみ使用できることに注意してください (ロケーターは軸データとビュー制限への参照を格納するため)。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)


t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)

fig, ax = plt.subplots()
ax.plot(t, s)

# Make a plot with major ticks that are multiples of 20 and minor ticks that
# are multiples of 5.  Label major ticks with '.0f' formatting but don't label
# minor ticks.  The string is used directly, the `StrMethodFormatter` is
# created automatically.
ax.xaxis.set_major_locator(MultipleLocator(20))
ax.xaxis.set_major_formatter('{x:.0f}')

# For the minor ticks, use no labels; default NullFormatter.
ax.xaxis.set_minor_locator(MultipleLocator(5))

plt.show()
メジャーマイナーデモ

大目盛りと小目盛りの自動目盛り選択。

対話型のパンとズームを使用して、目盛り間隔がどのように変化するかを確認します。メジャー インターバルに応じて、メジャー インターバルごとに 4 つまたは 5 つのマイナー ティック インターバルがあります。

AutoMinorLocator主間隔ごとに固定数の副間隔を指定する引数を に与えることができます。たとえばAutoMinorLocator(2)、主目盛りの間に単一の副目盛りができます。

t = np.arange(0.0, 100.0, 0.01)
s = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.xaxis.set_minor_locator(AutoMinorLocator())

ax.tick_params(which='both', width=2)
ax.tick_params(which='major', length=7)
ax.tick_params(which='minor', length=4, color='r')

plt.show()
メジャーマイナーデモ

スクリプトの合計実行時間: ( 0 分 1.333 秒)

Sphinx-Gallery によって生成されたギャラリー