开始使用免费开始使用

使用按钮切换注释

一家大型电商公司的销售部门希望您帮助他们理解并可视化其数据。

他们提供了月度销售数据,但使用不同的指标来评估月度表现:月度销售额(美元)和销售量(售出件数)。

他们希望在同一张图上查看所有信息,并且能够方便地打开或关闭注释,以便围绕这些信息进行讨论。

在本练习中,您将通过添加按钮来控制关键注释的显示与隐藏,帮助销售部门分析其销售数据。

您已获得一个 sales DataFrame,并且会为您预先设置一个 go.Figure() 对象。

本练习是课程的一部分

Python 中的 Plotly 数据可视化入门

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create the figure
fig = Figure()

# Create and add traces (done for you)
fig.add_trace(px.bar(sales, x="Month", y="Sales Volume", color_discrete_sequence=['blue']).data[0])
fig.add_trace(px.bar(sales, x="Month", y="Sales Value", color_discrete_sequence=['red']).data[0])

# Create annotations
value_annotations=[{'text': 'Sept was the best', 'showarrow': True, 'x': '____', 'y': 345397}]
volume_annotations=[{'text': 'Oct was the best', 'showarrow': True, 'x': '____', 'y': 71900}]

# Create buttons
my_buttons = [
{'label': "By Sales Value", 'method': "update", 'args': [{}, {'____': ____}]},
{'label': "By Sales Volume", 'method': "update", 'args': [{}, {'____': ____}]}]
编辑并运行代码