การปรับแต่งเพิ่มเติม
ลองดูสคริปต์อีกครั้งในส่วน # Additional Customizations จะเห็นว่ามีฟังก์ชัน plt.text() สองรายการ ซึ่งทำหน้าที่เพิ่มข้อความ "India" และ "China" ลงในกราฟ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Python ระดับกลาง
คำแนะนำการฝึกหัด
- เพิ่ม
plt.grid(True)ต่อจากการเรียกใช้plt.text()เพื่อให้กราฟแสดงเส้นกริด
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Scatter plot
plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2, c = col, alpha = 0.8)
# Previous customizations
plt.xscale('log')
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')
plt.xticks([1000,10000,100000], ['1k','10k','100k'])
# Additional customizations
plt.text(1550, 71, 'India')
plt.text(5700, 80, 'China')
# Add grid() call
# Show the plot
plt.show()