Get startedGet started for free

Finance line chart with custom time buttons

You have been engaged by an Excel-savvy finance trading company to help them jazz up their data visualization capabilities. Safe to say, Excel graphics aren't cutting it for them!

The fund is particularly interested in the electric vehicle company Tesla and how it has performed this year and wants a tool that can help them zoom to view key timeframes.

In this exercise, you will help the trading company by visualizing the opening stock price of Tesla over 2020 and create the following date-filter buttons:

  • 1WTD = The previous week (7 days to date)
  • 6MTD = The previous 6 months week (6 months to date)
  • YTD = The current year to date

You have a tesla DataFrame available that contains the necessary data.

This exercise is part of the course

Introduction to Data Visualization with Plotly in Python

View Course

Exercise instructions

  • Create a basic line chart of the tesla DataFrame using the Date and Open columns.
  • Create a list called fin_buttons containing the custom date-filter buttons mentioned above.
  • Update the figure using .update_layout() to construct buttons using the list you just created.

Hands-on interactive exercise

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

# Create the basic line chart
fig = px.____(data_frame=____, x=____, y=____, title="Tesla Opening Stock Prices")

# Create the financial buttons
fin_buttons = [
  {'count': ____, 'label': "1WTD", 'step': ____, 'stepmode': ____},
  {'count': ____, 'label': "6MTD", 'step': ____, 'stepmode': ____},
  {'count': ____, 'label': "YTD", 'step': ____, 'stepmode': ____}
]

# Create the date range buttons & show the plot
fig.update_layout({'xaxis': {____: {'buttons': ____}}})
fig.show()
Edit and Run Code