0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

add_axes()の引数を理解しよう〜初心者向けMatplotlib講座 #3〜

0
Last updated at Posted at 2026-04-20

前回はFigureとAxesの概念について学びました。今回はAxesを作るときに使ったadd_axes()の引数を詳しく見ていきましょう。

前回の記事はこちら

目次

  1. add_axes()の引数の構造
  2. 実際に引数を変えてみる
  3. はみ出しに注意!

add_axes()の引数の構造

前回使ったコードを振り返りましょう。

ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

リストの中に数値が4つありますね。これらは**Figure全体を1とした割合(0〜1)**で指定します。

fig.add_axes([left, bottom, width, height])
              └─┘   └────┘  └───┘  └────┘
             左端   下端    横幅   縦幅
            ←開始位置→  ←──サイズ──→
引数 意味
left 0.1 Figureの左端から10%の位置からAxesが始まる
bottom 0.1 Figureの下端から10%の位置からAxesが始まる
width 0.8 Axesの横幅はFigureの80%(右端の位置ではない!)
height 0.8 Axesの縦幅はFigureの80%(上端の位置ではない!)

最初は3つ目が「右端」、4つ目が「上端」だと思いがちですが、**開始位置からのサイズ(幅・高さ)**です。ここは間違えやすいポイントなので注意してください。

[0.1, 0.1, 0.8, 0.8]のときのイメージはこうなります。

Figure(全体)
┌──────────────────┐
│  ←10%→           │
│  ┌────────────┐ ↑│
│  │            │10%
│  │   Axes     │ ↓│
│  │  (80%×80%) │  │
│  │            │  │
│  └────────────┘  │
└──────────────────┘

left=0.1, bottom=0.1にしているおかげで、Axesの周囲に余白(10%分)が生まれ、軸ラベルなどが見切れないようになっています。

実際に引数を変えてみる

百聞は一見にしかず、実際にsinグラフで確かめてみましょう。

[0.1, 0.1, 0.8, 0.8](デフォルト的な指定)

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))
plt.show()

適度な余白があり、バランスよく表示されます。

2つのAxesを重ねる

add_axes()を複数回呼ぶと、1つのFigureに複数のAxesを置くこともできます。

fig = plt.figure()

# 大きいAxes
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# 小さいAxes(右上に重ねる)
ax2 = fig.add_axes([0.6, 0.6, 0.25, 0.25])

x = np.linspace(0, 10, 100)
ax1.plot(x, np.sin(x))
ax2.plot(x, np.cos(x))
plt.show()

表示結果

sinグラフ

位置を割合で指定できるからこそ、このような柔軟なレイアウトが実現できます。

はみ出しに注意!

引数を指定するときに1つ注意点があります。

たとえばこのような指定をするとどうなるでしょう?

ax = fig.add_axes([0.1, 0.1, 1.0, 1.0])
Figure(0.0 〜 1.0)
├──┬──────────────────────────┤
0.0 0.1                      1.1(範囲外!)
    ↑ここからAxes開始
    ←────── 幅100% ──────────────→

left(0.1) + width(1.0) = 1.1 となり、Figureの右端(1.0)を超えてしまいます。Matplotlibが自動で表示を調整してくれることもありますが、意図しないレイアウトになる可能性があるため、次の条件を守るようにしましょう。

意図通りに表示するための条件:

left + width  ≤ 1.0
bottom + height ≤ 1.0
指定 left + width 結果
[0.1, 0.1, 0.8, 0.8] 0.1 + 0.8 = 0.9 ✅ 収まる
[0.1, 0.1, 1.0, 1.0] 0.1 + 1.0 = 1.1 ⚠️ 範囲外(意図しない表示になる可能性あり)

※ MatplotlibやJupyter/Google Colabが自動で表示を調整することがあるため、範囲外の指定でも一見正常に見えることがあります。

まとめ

  • add_axes([left, bottom, width, height])の4つの引数はすべてFigure全体を1とした割合
  • leftbottomAxesの開始位置widthheightAxesのサイズ
  • left + width ≤ 1.0bottom + height ≤ 1.0を守ると意図通りのレイアウトになる
  • add_axes()を複数回呼ぶことで、1つのFigureに複数のAxesを配置できる

次回は実際にグラフにタイトルや軸ラベルを追加する方法を見ていきます!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?