開始使用免費開始

matplotlib 的子圖(subfigures)

有時你會想在同一個圖形中放入多個子圖。

你可以使用 plot.subplots() 函式同時建立 figure 與 axes。 這個函式會回傳 figure 與 axes 物件:

import matplotlib.pyplot as plt

# Create a figure and axes 
fig, (ax1, ax2) = plt.subplots(number_of_rows, number_of_columns)

接著就能用這些 axes 物件來繪圖:

# Use the axes to plot visualizations
ax1.hist(df['column_1'])
ax2.hist(df['column_2'])
plt.show()

本練習屬於課程

給 R 使用者的 Python

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

import matplotlib.pyplot as plt

# Create a figure with 1 axes
fig, ax = plt.____(1, 1)

# Plot a scatter plot in the axes
____.scatter(tips____, tips____)
plt.show()
編輯並執行程式碼