Matplotlibの目盛りの設定#

公開日

Matplotlibの目盛りの設定(補助目盛りや目盛り線など)について解説します。

補助目盛り#

補助目盛りを表示するには、Axes.minorticks_on()メソッドを用います。

import matplotlib.pyplot as plt

x = [1, 3, 2]

# 補助目盛りなし
fig, ax = plt.subplots()
ax.plot(x)
plt.show()

# 補助目盛りあり
fig, ax = plt.subplots()
ax.plot(x)
ax.minorticks_on()
plt.show()
../_images/9c48f44793fef393f87aff6145071f2ba7ae707fea8995c408bd370e70e1fccd.png ../_images/a737424697fe617a3bc5adc7e41e7a88b0491ee11b3a6a05c7df7d6bff1b9123.png

目盛り線#

目盛り線を引くには、Axes.grid()メソッドを用います。このメソッドの主なオプションは以下の通りです。

オプション

説明

axis

str

軸を選択('both', 'x', 'y')

which

str

目盛り線の種類を指定。'major'(主目盛り)、'minor'(補助目盛り)、'both'(両方の目盛り)

linewidth

float

線の太さ

color

str

線の色

linestyle

str

線の種類

axisオプションを使ってx軸のみ目盛り線を引く例を以下に示します。

fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.grid()
plt.show()

# x軸のみ目盛り線を引く
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.grid(axis="x")
plt.show()
../_images/19fbf4be30de2fede35cf1f74b2646453954e4fd121e6eae9aecc15097ce4827.png ../_images/98d9044c19f4a906b4507fe4d8c02aa79901273d2e63eac975dd85548a80f929.png

grid()メソッドのwhichオプションを指定しない場合、主目盛り線のみ引かれます。補助目盛りにも線を引くには、ax.minorticks_on()で補助目盛りをONにした後、ax.grid(which="both")を実行します。

fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.minorticks_on() # 補助目盛りを表示する
ax.grid() # デフォルトでは主目盛り線のみ表示される
plt.show()

# 主目盛りと補助目盛りの両方に目盛り線を引く
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.minorticks_on()
ax.grid(which="both")
plt.show()
../_images/23d4f37fd3959a64fac23420ea800d77d3fbe9f71246491a2e605166508cc78e.png ../_images/6cf55a2e08e69ab110a41abaf8e9eb2e2c465501c683f2523c0ad68554bf7f57.png

目盛り線の太さはlinewidthオプション、色はcolorオプション、種類はlinestyleオプションでそれぞれ変更できます。例を以下に示します。

fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.grid(linewidth=1.5, color="green", linestyle="--")
plt.show()
../_images/da60d81c8716f2ce70189d13d0f031e6d2dd5a7cbfcee362e445eb40749fc95b.png

tick_params#

目盛りの詳細な設定をするには、Axes.tick_params()メソッドを用います。このメソッドの主なオプションを以下に示します。

オプション

説明

axis

str

軸を選択('both', 'x', 'y')

which

str

目盛り線の種類を指定。'major'(主目盛り)、'minor'(補助目盛り)、'both'(両方の目盛り)

direction

str

目盛りの位置。'in', 'out', 'inout'

length

float

目盛りの長さ

width

float

目盛りの幅

color

str

目盛りの色

pad

float

目盛りと目盛りラベルの隙間

labelsize

float

目盛りラベルの大きさ

labelcolor

str

目盛りラベルの色

colors

str

目盛りと目盛りラベルの色

zorder

float

描画する順序

labelrotation

float

目盛りラベルの回転角度(半時計回りに回転。単位は度)

なお、Axes.tick_params()メソッドで補助目盛りの設定をする場合、Axes.minorticks_on()メソッドを実行する必要があります。

目盛りの向き#

目盛りの向きはdirectionオプションで指定できます。inで内側、outで外側(デフォルト)、inoutで両方となります。

# 内側
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(direction='in')
plt.show()

# 両方
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(direction='inout')
plt.show()
../_images/3ee2699df1fbefb0551fd7fd55ef2485da8c6c5d9c3d94b42e077ef11ee7da3c.png ../_images/4798814abd62a49e5f03066575a65bc8bee3ca7ed9a5dcfa317ab3a192f63859.png

目盛りとラベルの色#

colorオプションでは目盛り線のみ、colorsオプションでは目盛り線と目盛りラベルの両方の色を変更できます。また、labelcolorオプションでは、目盛りラベルのみ色を変更できます。

# 目盛り線を赤色に設定
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(color="red", width=3)
plt.show()

# 目盛り線と目盛りラベルを赤色に設定
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(colors="red", width=3)
plt.show()

# 目盛りラベルを赤色に設定
fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(labelcolor="red", width=3)
plt.show()
../_images/d80ba368d414853a878b50487bd903aff88fc30724e4e6661b1105e0802b1e64.png ../_images/7e29a6d84df62ab99db06d87d12d4101fa38007829f5b7f3e916e5743c9de594.png ../_images/61d14163ef4246623e8f5c315da5ebc14e8709ba6396f799e94713af7f92fef4.png

目盛りラベルの回転#

labelrotationオプションによって目盛りラベルを回転できます。半時計回りに回転し、単位は度です。

fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(labelrotation=45)
plt.show()
../_images/d5da50e58f60d0f6ae26f9045ec9ea59c1be924be9729f584f1eef2b7ca2949d.png

x軸またはy軸のみの目盛りラベルを回転させる場合、ax.tick_params()メソッドのaxisオプションで指定します。

fig, ax = plt.subplots()
ax.plot([1, 3, 2])
ax.tick_params(axis="x", labelrotation=45)
ax.tick_params(axis="y", labelrotation=-10)
plt.show()
../_images/9815ae20fef66d1f54d65879d7ad295eae290d5fb0a26702e531e931a41cb365.png