การสร้าง small multiples ด้วย plt.subplots
Small multiples ใช้สำหรับแสดงชุดข้อมูลหลายชุดเคียงกัน ใน Matplotlib สามารถสร้าง small multiples ได้ด้วยฟังก์ชัน plt.subplots() อาร์กิวเมนต์แรกคือจำนวนแถว และอาร์กิวเมนต์ที่สองคือจำนวนคอลัมน์ของอาร์เรย์ Axes objects ในแบบฝึกหัดนี้ จะใช้ข้อมูลของเมือง Austin และ Seattle เพื่อฝึกสร้างและเติมข้อมูลลงในอาร์เรย์ของ subplots
ข้อมูลถูกกำหนดให้ในรูปแบบ DataFrame ได้แก่ seattle_weather และ austin_weather ซึ่งแต่ละชุดมีคอลัมน์ "MONTH" และ "MLY-PRCP-NORMAL" (ปริมาณน้ำฝนเฉลี่ย) รวมถึง "MLY-TAVG-NORMAL" (อุณหภูมิเฉลี่ย) ในแบบฝึกหัดนี้ จะพล็อตปริมาณน้ำฝนเฉลี่ยรายเดือนและอุณหภูมิเฉลี่ยของแต่ละเมืองลงใน subplot แยกกัน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น
คำแนะนำการฝึกหัด
- สร้าง Figure และอาร์เรย์ของ subplots ที่มี 2 แถวและ 2 คอลัมน์
- เข้าถึง Axes ด้านบนซ้ายที่ index 0, 0 แล้วพล็อตปริมาณน้ำฝนของ Seattle
- ที่ด้านบนขวา (index 0, 1) พล็อตอุณหภูมิของ Seattle
- ที่ด้านล่างซ้าย (1, 0) และด้านล่างขวา (1, 1) พล็อตปริมาณน้ำฝนและอุณหภูมิของ Austin ตามลำดับ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a Figure and an array of subplots with 2 rows and 2 columns
fig, ax = plt.subplots(____, ____)
# Addressing the top left Axes as index 0, 0, plot month and Seattle precipitation
ax[0, 0].plot(____, ____)
# In the top right (index 0,1), plot month and Seattle temperatures
ax[0, 1].plot(____, ____)
# In the bottom left (1, 0) plot month and Austin precipitations
ax[____].plot(____, ____)
# In the bottom right (1, 1) plot month and Austin temperatures
ax[____].plot(____, ____)
plt.show()