Matplotlib Fig レベルのテキストと注釈関数
text() と annotate() の他に、Matplotlib は Figure レベルのテキスト関数、テーブル追加、矢印描画も提供しています。
関数一覧
| 関数 | 機能 |
|---|---|
| figtext() | Figure レベル(Axes 以外)でテキストを追加 |
| figlegend() | Figure レベルで凡例を追加(Axes 横断) |
| table() | Axes にテーブルを追加 |
| arrow() | 単純な矢印を追加 |
figtext() - Figure レベルのテキスト
matplotlib.pyplot.figtext(x, y, s, fontdict=None, **kwargs)
text() と似ていますが、座標系はデータ座標ではなく Figure 座標(0-1)です。
実例
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([0, 1], [0, 1], 'steelblue')
ax.set_title('Axes-level (text vs figtext)')
# Axes レベルのテキスト(データ座標)
ax.text(0.3, 0.7, 'Axes text (data coords)',
fontsize=12, color='blue')
# Figure レベルのテキスト(Figure 相対座標 0-1)
plt.figtext(0.5, 0.02,
'Figure text at bottom center (Figure coords)',
ha='center', fontsize=10, color='gray')
plt.figtext(0.02, 0.95,
'Top left',
ha='left', va='top',
fontsize=12, fontweight='bold', color='red')
plt.show()
import numpy as np
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([0, 1], [0, 1], 'steelblue')
ax.set_title('Axes-level (text vs figtext)')
# Axes レベルのテキスト(データ座標)
ax.text(0.3, 0.7, 'Axes text (data coords)',
fontsize=12, color='blue')
# Figure レベルのテキスト(Figure 相対座標 0-1)
plt.figtext(0.5, 0.02,
'Figure text at bottom center (Figure coords)',
ha='center', fontsize=10, color='gray')
plt.figtext(0.02, 0.95,
'Top left',
ha='left', va='top',
fontsize=12, fontweight='bold', color='red')
plt.show()
figlegend() - Figure レベルの凡例
matplotlib.pyplot.figlegend(*args, **kwargs)
複数の Axes で凡例を共有する必要がある場合、figlegend() は Figure レベルで凡例を配置できます。
実例
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# 各 Axes に同じ 2 本の曲線を描画するが、それぞれには凡例を追加しない
ax1.plot(x, np.sin(x), 'b-', label='sin(x)')
ax1.plot(x, np.cos(x), 'r--', label='cos(x)')
ax1.set_title('Plot 1')
ax2.plot(x, np.sin(x), 'b-', label='sin(x)')
ax2.plot(x, np.cos(x), 'r--', label='cos(x)')
ax2.set_title('Plot 2')
# Figure レベルの凡例(Figure の外側上部に配置)
fig.legend(loc='outside upper center', ncol=2,
fontsize=11, frameon=True)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# 各 Axes に同じ 2 本の曲線を描画するが、それぞれには凡例を追加しない
ax1.plot(x, np.sin(x), 'b-', label='sin(x)')
ax1.plot(x, np.cos(x), 'r--', label='cos(x)')
ax1.set_title('Plot 1')
ax2.plot(x, np.sin(x), 'b-', label='sin(x)')
ax2.plot(x, np.cos(x), 'r--', label='cos(x)')
ax2.set_title('Plot 2')
# Figure レベルの凡例(Figure の外側上部に配置)
fig.legend(loc='outside upper center', ncol=2,
fontsize=11, frameon=True)
plt.show()
table() - Axes にテーブルを追加
matplotlib.pyplot.table(cellText=None, cellColours=None,
cellLoc='right', colWidths=None, rowLabels=None,
rowColours=None, rowLoc='left', colLabels=None,
colColours=None, colLoc='center', loc='bottom',
bbox=None, edges='closed', **kwargs)
実例
import matplotlib.pyplot as plt
import numpy as np
# 表示するデータ
data = [[85, 90, 78, 92],
[76, 88, 82, 85],
[90, 93, 88, 95]]
row_labels = ['Student A', 'Student B', 'Student C']
col_labels = ['Math', 'English', 'Science', 'History']
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# 比較グラフを描画
x = np.arange(len(col_labels))
for i, (row, label) in enumerate(zip(data, row_labels)):
ax.plot(x, row, 'o-', label=label, markersize=8)
ax.set_xticks(x)
ax.set_xticklabels(col_labels)
ax.set_title('Student Scores')
ax.legend()
ax.grid(True, alpha=0.3)
# グラフの下にテーブルを追加
table = ax.table(cellText=data,
rowLabels=row_labels,
colLabels=col_labels,
cellLoc='center',
loc='bottom',
bbox=[0, -0.35, 1, 0.25]) # [left, bottom, width, height]
plt.show()
import numpy as np
# 表示するデータ
data = [[85, 90, 78, 92],
[76, 88, 82, 85],
[90, 93, 88, 95]]
row_labels = ['Student A', 'Student B', 'Student C']
col_labels = ['Math', 'English', 'Science', 'History']
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
# 比較グラフを描画
x = np.arange(len(col_labels))
for i, (row, label) in enumerate(zip(data, row_labels)):
ax.plot(x, row, 'o-', label=label, markersize=8)
ax.set_xticks(x)
ax.set_xticklabels(col_labels)
ax.set_title('Student Scores')
ax.legend()
ax.grid(True, alpha=0.3)
# グラフの下にテーブルを追加
table = ax.table(cellText=data,
rowLabels=row_labels,
colLabels=col_labels,
cellLoc='center',
loc='bottom',
bbox=[0, -0.35, 1, 0.25]) # [left, bottom, width, height]
plt.show()
arrow() - 矢印の追加
matplotlib.pyplot.arrow(x, y, dx, dy, **kwargs)
arrow() は単純な矢印を追加するためのショートカットです。複雑な矢印注釈には annotate() を使用することをお勧めします。
実例
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
# 座標系を描画
ax.spines[['left', 'bottom']].set_position(('data', 0))
ax.spines[['top', 'right']].set_visible(False)
ax.set_xlim(-1, 5)
ax.set_ylim(-1, 5)
# さまざまな矢印
ax.arrow(0, 0, 3, 0, head_width=0.2, head_length=0.2,
fc='red', ec='red', label='Right 3 units')
ax.arrow(0, 0, 0, 3, head_width=0.2, head_length=0.2,
fc='blue', ec='blue', label='Up 3 units')
ax.arrow(0, 0, 2, 2, head_width=0.2, head_length=0.2,
fc='green', ec='green', label='Diagonal')
ax.set_title('arrow() - Simple Arrows')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
fig, ax = plt.subplots(figsize=(7, 5), layout='constrained')
# 座標系を描画
ax.spines[['left', 'bottom']].set_position(('data', 0))
ax.spines[['top', 'right']].set_visible(False)
ax.set_xlim(-1, 5)
ax.set_ylim(-1, 5)
# さまざまな矢印
ax.arrow(0, 0, 3, 0, head_width=0.2, head_length=0.2,
fc='red', ec='red', label='Right 3 units')
ax.arrow(0, 0, 0, 3, head_width=0.2, head_length=0.2,
fc='blue', ec='blue', label='Up 3 units')
ax.arrow(0, 0, 2, 2, head_width=0.2, head_length=0.2,
fc='green', ec='green', label='Diagonal')
ax.set_title('arrow() - Simple Arrows')
ax.legend()
ax.grid(True, alpha=0.3)
plt.show()
その他の拡張
Matplotlib リファレンスドキュメント