Matplotlib の軸設定関数
この関数群は、軸の範囲、スケール、目盛り、グリッド線、外観を制御するために使用します。
関数一覧
| 関数 | 機能 |
|---|---|
| xlim() / ylim() | x/y 軸の範囲を取得または設定 |
| xscale() / yscale() | x/y 軸のスケールを設定:'linear'、'log'、'symlog'、'logit' |
| xticks() / yticks() | x/y 軸の目盛り位置とラベルを取得または設定 |
| tick_params() | 目盛りの外観パラメータを調整 |
| ticklabel_format() | 目盛りラベルの形式を設定(科学表記法など) |
| locator_params() | 目盛りロケーターのパラメータを制御 |
| minorticks_on() / minorticks_off() | 補助目盛りを表示/非表示 |
| rgrids() | 極座標グラフの動径方向グリッド線を設定 |
| thetagrids() | 極座標グラフの角度方向グリッド線を設定 |
| grid() | グリッド線をオン/オフ |
| axis() | 軸の範囲と外観を簡単に設定 |
| box() | Axes の枠線をオン/オフ |
| autoscale() | 軸を自動スケーリング |
xlim() / ylim() - 軸の範囲
matplotlib.pyplot.xlim(*args, **kwargs) # 获取或设置 matplotlib.pyplot.ylim(*args, **kwargs)
xscale() / yscale() - 軸のスケール
matplotlib.pyplot.xscale(value, **kwargs) matplotlib.pyplot.yscale(value, **kwargs) # value: 'linear', 'log', 'symlog', 'logit', 'function', 'asinh', ...
xticks() / yticks() - 目盛り
matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) matplotlib.pyplot.yticks(ticks=None, labels=None, **kwargs)
tick_params() - 目盛りの外観
matplotlib.pyplot.tick_params(axis='both', **kwargs) # 常用参数: labelsize, labelcolor, rotation, direction, length, width, colors
ticklabel_format() - 目盛りラベルの形式
matplotlib.pyplot.ticklabel_format(*, axis='both', style='',
scilimits=None, useOffset=None, useLocale=None, useMathText=None)
grid() - グリッド線
matplotlib.pyplot.grid(visible=None, which='major', axis='both',
**kwargs)
axis() - 軸の外観を簡単に設定する関数
matplotlib.pyplot.axis(*args, **kwargs) # 可接受字符串: 'on', 'off', 'equal', 'scaled', 'tight', 'auto', 'square' # 可接受列表: [xmin, xmax, ymin, ymax]
box() - 枠線
matplotlib.pyplot.box(on=None)
autoscale() - 自動スケーリング
matplotlib.pyplot.autoscale(enable=True, axis='both', tight=None)
locator_params() / minorticks
matplotlib.pyplot.locator_params(axis='both', tight=None, **kwargs) matplotlib.pyplot.minorticks_on() matplotlib.pyplot.minorticks_off()
使用例
例1:総合的な軸設定
サンプル
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 100, 200)
y = x**2
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(14, 4),
layout='constrained')
# 左のグラフ:線形座標 + カスタム目盛りとグリッド
ax1.plot(x, y, 'steelblue')
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 10000)
ax1.set_xticks([0, 25, 50, 75, 100])
ax1.set_yticks([0, 2500, 5000, 7500, 10000])
ax1.grid(True, linestyle='--', alpha=0.4)
ax1.set_title('Linear: custom ticks')
# 中央のグラフ:対数座標
ax2.loglog(x, y, 'coral')
ax2.grid(True, which='both', linestyle=':', alpha=0.4)
ax2.set_title('loglog()')
# 右のグラフ:科学表記法
ax3.plot(x, y, 'green')
ax3.ticklabel_format(style='sci', axis='y',
scilimits=(0, 0))
ax3.minorticks_on()
ax3.grid(True, which='major', alpha=0.5)
ax3.grid(True, which='minor', alpha=0.15)
ax3.set_title('Scientific notation + minor ticks')
plt.show()
import numpy as np
x = np.linspace(0.1, 100, 200)
y = x**2
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(14, 4),
layout='constrained')
# 左のグラフ:線形座標 + カスタム目盛りとグリッド
ax1.plot(x, y, 'steelblue')
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 10000)
ax1.set_xticks([0, 25, 50, 75, 100])
ax1.set_yticks([0, 2500, 5000, 7500, 10000])
ax1.grid(True, linestyle='--', alpha=0.4)
ax1.set_title('Linear: custom ticks')
# 中央のグラフ:対数座標
ax2.loglog(x, y, 'coral')
ax2.grid(True, which='both', linestyle=':', alpha=0.4)
ax2.set_title('loglog()')
# 右のグラフ:科学表記法
ax3.plot(x, y, 'green')
ax3.ticklabel_format(style='sci', axis='y',
scilimits=(0, 0))
ax3.minorticks_on()
ax3.grid(True, which='major', alpha=0.5)
ax3.grid(True, which='minor', alpha=0.15)
ax3.set_title('Scientific notation + minor ticks')
plt.show()
例2:tick_params の詳細設定
サンプル
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# 目盛りの外観を詳細に設定
ax.tick_params(axis='x', # x 軸のみ
rotation=45, # ラベルを45度回転
labelsize=10, # ラベルのサイズ
labelcolor='blue', # ラベルの色
direction='in', # 目盛りを内側に向ける
length=6, # 目盛りの長さ
width=1.5) # 目盛りの幅
ax.tick_params(axis='y',
labelsize=12,
labelcolor='red',
direction='inout', # 内側と外側の両方に目盛り
length=8,
width=2,
colors='red') # 目盛りとラベルの両方を赤色に
ax.set_title('tick_params() Customization')
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# 目盛りの外観を詳細に設定
ax.tick_params(axis='x', # x 軸のみ
rotation=45, # ラベルを45度回転
labelsize=10, # ラベルのサイズ
labelcolor='blue', # ラベルの色
direction='in', # 目盛りを内側に向ける
length=6, # 目盛りの長さ
width=1.5) # 目盛りの幅
ax.tick_params(axis='y',
labelsize=12,
labelcolor='red',
direction='inout', # 内側と外側の両方に目盛り
length=8,
width=2,
colors='red') # 目盛りとラベルの両方を赤色に
ax.set_title('tick_params() Customization')
ax.grid(True, alpha=0.3)
plt.show()
例3:axis() と box()
サンプル
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
fig, axes = plt.subplots(2, 2, figsize=(8, 8),
layout='constrained')
# 'equal':x と y の単位長さが等しい
axes[0,0].plot(x, np.sin(x))
axes[0,0].axis('equal')
axes[0,0].set_title("axis('equal')")
# 'square':正方形の Axes
axes[0,1].plot(x, np.cos(x))
axes[0,1].axis('square')
axes[0,1].set_title("axis('square')")
# 'tight':データにぴったり合わせる
axes[1,0].plot(x, x**2)
axes[1,0].axis('tight')
axes[1,0].set_title("axis('tight')")
# box(False) で枠線を削除
axes[1,1].plot(x, np.sin(x))
axes[1,1].box(False)
axes[1,1].set_title('box(False) - No frame')
plt.show()
print("サンプル: axis config demo")
import numpy as np
x = np.linspace(-5, 5, 100)
fig, axes = plt.subplots(2, 2, figsize=(8, 8),
layout='constrained')
# 'equal':x と y の単位長さが等しい
axes[0,0].plot(x, np.sin(x))
axes[0,0].axis('equal')
axes[0,0].set_title("axis('equal')")
# 'square':正方形の Axes
axes[0,1].plot(x, np.cos(x))
axes[0,1].axis('square')
axes[0,1].set_title("axis('square')")
# 'tight':データにぴったり合わせる
axes[1,0].plot(x, x**2)
axes[1,0].axis('tight')
axes[1,0].set_title("axis('tight')")
# box(False) で枠線を削除
axes[1,1].plot(x, np.sin(x))
axes[1,1].box(False)
axes[1,1].set_title('box(False) - No frame')
plt.show()
print("サンプル: axis config demo")
その他の拡張