Subfigure ใน matplotlib
บางครั้งเราอาจต้องการรวมกราฟย่อยหลายรูปไว้ในรูปเดียวกัน
ทำได้โดยสร้าง figure และ axes พร้อมกันในคราวเดียวด้วยฟังก์ชัน plot.subplots()
ฟังก์ชันนี้จะคืนค่าออบเจกต์ 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()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Python สำหรับผู้ใช้ R
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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()