0Pricing
Pandas & NumPy Academy · レッスン

FigureとAxesの構造

Figure-Axes-Artistの階層を理解し、plt.subplots()でプロットを作成して、ステートレスAPIとオブジェクト指向APIの違いを見分けます。

「FigureとAxesの構造」はCoddyKit上の無料Pandas & NumPy Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPandas & NumPy Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Pandas & NumPy Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Learn Matplotlib Architecture?

Pandas and Seaborn both use Matplotlib internally. Understanding Matplotlib's object hierarchy lets you control every aspect of a plot — from the overall canvas to individual tick marks — and lets you combine multiple charts in a single figure. Without understanding the Figure-Axes model, customising plots quickly becomes a frustrating exercise in guessing which function to call.

The Figure: The Canvas

A Figure is the outermost container — the entire window or image file. It holds one or more Axes objects (the actual plot areas), plus a title, shared legends, and whitespace. You create a Figure with plt.figure() or implicitly through plt.subplots(). Most properties set on the Figure affect the whole canvas: size, background colour, overall title.

import matplotlib.pyplot as plt
import numpy as np

# Create a Figure with a specific size
fig = plt.figure(figsize=(8, 4))  # width=8 inches, height=4 inches
print(type(fig))  # matplotlib.figure.Figure
print('Figure size:', fig.get_size_inches())
plt.close()  # close to avoid display in scripts

The Axes: Where Plotting Happens

An Axes object is a single plot area within a Figure. It contains the x-axis, y-axis, tick marks, grid lines, and all the visual elements (lines, bars, scatter points). Most plotting calls are methods on an Axes object: ax.plot(), ax.bar(), ax.scatter(). One Figure can contain multiple Axes, arranged as a grid of subplots.

fig, ax = plt.subplots()  # Creates Figure + one Axes
print(type(ax))  # matplotlib.axes.Axes

x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))  # plot on the Axes
plt.tight_layout()
# plt.show()
plt.close()

plt.subplots(): Create Figure and Axes Together

plt.subplots(nrows, ncols) is the recommended way to create a Figure with one or more Axes in a grid layout. It returns a tuple of (fig, ax) (or (fig, axes_array) for multiple subplots). The figsize parameter sets the total canvas size in inches. Always unpack the result into named variables for clarity.

# Single plot
fig, ax = plt.subplots(figsize=(6, 4))

# 2 rows, 1 column
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 8))

# 2 rows, 2 columns
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
ax_top_left = axes[0, 0]
ax_top_right = axes[0, 1]
print('axes shape:', axes.shape)  # (2, 2)
plt.close('all')

Object-Oriented vs Stateful API

Matplotlib has two usage styles. The stateful (pyplot) API uses plt.plot(), plt.xlabel() etc. and acts on the 'current' axes — convenient for quick interactive exploration. The object-oriented (OO) API explicitly calls methods on ax objects: ax.plot(), ax.set_xlabel(). The OO API is strongly preferred for production code and multi-panel figures because it is unambiguous about which axes is being modified.

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

# Stateful API (convenient but fragile)
# plt.plot(x, y)
# plt.xlabel('x')
# plt.title('Squares')

# Object-oriented API (preferred)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlabel('x')
ax.set_title('Squares')
plt.close()

Artists: The Building Blocks

Everything visible in a Matplotlib plot is an Artist: the Figure, Axes, lines, text, patches (rectangles for bars), markers. The Figure-Axes-Artist hierarchy flows from the outermost container down to the smallest visual element. Understanding this hierarchy helps you locate and modify any element programmatically — for example, changing the colour of a specific bar after the chart is drawn.

fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], [1, 4, 9], color='blue', linewidth=2)

# Introspect the hierarchy
print('Figure:', type(fig))
print('Axes:', type(ax))
print('Line Artist:', type(line))
print('Line color:', line.get_color())
plt.close()

Axis Objects: XAxis and YAxis

Each Axes contains two Axis objects (not to be confused with Axes): ax.xaxis and ax.yaxis. These control ticks, tick labels, and the spine (the visible line). You can set axis limits with ax.set_xlim() and ax.set_ylim(), control tick frequency with ax.set_xticks(), and format labels with ax.xaxis.set_major_formatter().

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 40, 30, 70])

ax.set_xlim(0, 5)
ax.set_ylim(0, 80)
ax.set_xticks([1, 2, 3, 4])
ax.set_xticklabels(['Q1', 'Q2', 'Q3', 'Q4'])
ax.set_xlabel('Quarter')
ax.set_ylabel('Revenue')
plt.close()

Shared Axes for Multi-Panel Plots

When creating a grid of subplots, pass sharex=True or sharey=True to plt.subplots() to link the axis scales across panels. This means zooming or panning one subplot automatically updates all panels sharing that axis — essential for comparing time series across multiple groups where the same x-axis range should be shown in every panel.

import numpy as np

x = np.arange(10)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(6, 5))

ax1.plot(x, x**2, color='blue')
ax1.set_title('Squares')

ax2.plot(x, x**3, color='red')
ax2.set_title('Cubes')

fig.suptitle('Powers of x')  # shared figure title
plt.tight_layout()
plt.close()

Spacing with tight_layout()

plt.tight_layout() (or fig.tight_layout()) automatically adjusts subplot parameters to minimise overlap between labels, titles, and tick marks. Call it just before plt.show() or fig.savefig(). For fine-grained control, use plt.subplots_adjust(hspace=, wspace=) to set the vertical and horizontal space between subplots manually.

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

for i, ax in enumerate(axes.flat):
    ax.plot([1, 2, 3], [i, i+1, i+2])
    ax.set_title(f'Panel {i+1}')
    ax.set_xlabel('x axis label')
    ax.set_ylabel('y axis label')

fig.suptitle('Grid of Plots', y=1.02)
plt.tight_layout()  # prevents overlapping labels
plt.close()

Accessing Axes from subplots()

When plt.subplots(nrows, ncols) returns multiple axes, they are packaged in a NumPy array with shape (nrows, ncols). Index them as you would any 2D array: axes[0, 1] for the first row, second column. Use axes.flat to iterate over all axes in row-major order regardless of the grid shape — very convenient for applying the same formatting to every panel.

fig, axes = plt.subplots(2, 3, figsize=(12, 6))

# Access by row, column
axes[0, 0].set_title('Top Left')
axes[1, 2].set_title('Bottom Right')

# Iterate over all axes
for ax in axes.flat:
    ax.set_xlabel('Time')
    ax.set_ylabel('Value')

plt.tight_layout()
plt.close()

Removing Unused Axes

When you create a grid of subplots but do not use every panel, the empty axes still show frame borders. Call ax.set_visible(False) or fig.delaxes(ax) to hide or remove unused axes. A common pattern is to iterate over axes.flat and hide all panels beyond the number of data series you are plotting, keeping the figure clean.

fig, axes = plt.subplots(2, 3, figsize=(12, 6))
data_panels = 4  # only 4 plots needed in a 2x3 grid

for i, ax in enumerate(axes.flat):
    if i < data_panels:
        ax.plot([1, 2, 3], [i, i+1, i+2])
        ax.set_title(f'Panel {i+1}')
    else:
        ax.set_visible(False)  # hide extra panels

plt.tight_layout()
plt.close()

Quick Check

Test your understanding of Matplotlib's Figure and Axes architecture from this lesson.

Lesson Recap

In this lesson you learned: the Figure is the entire canvas and the Axes is a single plot area within it; plt.subplots() creates both together and returns (fig, ax); the object-oriented API (ax.plot(), ax.set_xlabel()) is preferred over the stateful pyplot API; and tight_layout() prevents overlapping labels in multi-panel figures. Next up we create line plots and scatter plots with full control over style.

よくある質問

「FigureとAxesの構造」レッスンは無料ですか?

はい。「FigureとAxesの構造」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Pandas & NumPy Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Pandas & NumPy Academyコースには全4レッスンが含まれています。

「FigureとAxesの構造」で何を学びますか?

Figure-Axes-Artistの階層を理解し、plt.subplots()でプロットを作成して、ステートレスAPIとオブジェクト指向APIの違いを見分けます。 ブラウザで直接実行するハンズオンコードでPandas & NumPy Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Pandas & NumPy Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPandas & NumPy Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「FigureとAxesの構造」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPandas & NumPy Academyレッスンでコードを書いて実行できますか?

はい。すべてのPandas & NumPy Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. FigureとAxesの構造
  2. 折れ線グラフと散布図
  3. 棒グラフとヒストグラム
  4. ラベル、タイトル、図の保存
← Pandas & NumPy Academyに戻る