Get startedGet started for free

Changing annotations with buttons

A large e-commerce company's sales department has asked you to assist them in understanding and visualizing their data.

They have provided monthly sales data but use different metrics to determine monthly performance: monthly sales value (in dollars) and sales volume (number of items sold).

They would like to view all this information on the same plot but easily be able to toggle annotations on and off to facilitate their discussion around this.

In this exercise, you will help the sales department analyze their sales data with buttons to turn key annotations on or off.

You have been provided a sales DataFrame and a go.Figure() object will be set up for you.

This exercise is part of the course

Introduction to Data Visualization with Plotly in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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': [{}, {'____': ____}]}]
Edit and Run Code