为储蓄图添加注释
过去 30 周,您一直努力为第一辆车积攒储蓄。不过,需要补充一些背景信息来解释储蓄的一次下滑,以及之后每两周储蓄额的大幅增加。
您的任务是为按周的储蓄余额柱状图添加注释,并在图中加入两个关键注释,解释发生了什么。
已经为您基于 savings DataFrame(其中 x 为 Week)创建了图形 fig。
本练习是课程的一部分
Python 中的 Plotly 数据可视化入门
练习说明
- 在第 10 周(储蓄 400)创建
loss_annotation,文本为"Urgent House Repairs"。 - 在第 18 周(储蓄 2500)创建
gain_annotation,文本为"New Job"。 - 使用
update_layout()将这两个注释同时添加到fig。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()