การเพิ่ม Annotation ให้กราฟเงินออม
คุณทุ่มเทมาตลอด 30 สัปดาห์เพื่อเก็บเงินซื้อรถคันแรก แต่กราฟยังขาดบริบทที่จะอธิบายว่าทำไมเงินออมถึงลดลงในช่วงหนึ่ง และเพิ่มขึ้นอย่างมากในอีกช่วง
ในแบบฝึกหัดนี้ ให้เพิ่ม annotation สองจุดลงในแผนภูมิแท่งที่แสดงยอดเงินออมรายสัปดาห์ เพื่ออธิบายสิ่งที่เกิดขึ้น
กราฟ fig ถูกสร้างจาก DataFrame savings (โดยใช้ Week เป็นแกน x) ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแสดงผลข้อมูลด้วย Plotly ใน Python
คำแนะนำการฝึกหัด
- สร้าง
loss_annotationที่สัปดาห์ที่ 10 (เงินออม 400) พร้อมข้อความ"Urgent House Repairs" - สร้าง
gain_annotationที่สัปดาห์ที่ 18 (เงินออม 2500) พร้อมข้อความ"New Job" - เพิ่ม annotation ทั้งสองลงใน
figโดยใช้update_layout()
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create the first annotation
loss_annotation = dict(
x=____, y=____, text="____",
font=dict(color="black"), showarrow=True, arrowhead=4
)
# Create the second annotation
gain_annotation = dict(
x=____, y=____, text="____",
font=dict(color="black"), showarrow=True, arrowhead=4,
)
# Add annotations to the figure
fig.update_layout(annotations=[____, ____])
# Show the plot
fig.show()