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 a pharmaceutical company and how it has performed this year and wants a tool to help them zoom in on key timeframes.
In this exercise, you will help the trading company by visualizing the opening stock price of the company over 2020 and creating 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 stock_price
DataFrame available that contains the necessary data.
This exercise is part of the course
Introduction to Data Visualization with Plotly in Python
Exercise instructions
- Create a line chart of the
stock_price
DataFrame using theDate
andOpen
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 your created list.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a line chart
fig = px.____(stock_price, x='____', y='____', title='Opening Stock Prices')
# Create the financial buttons
fin_buttons = [
{'count': ____, 'label': "1WTD", 'step': "____", 'stepmode': "todate"},
{'count': ____, 'label': "6MTD", 'step': "____", 'stepmode': "todate"},
{'count': ____, 'label': "YTD", 'step': "____", 'stepmode': "todate"}
]
# Add the buttons
fig.update_layout(dict(
xaxis=dict(
____=dict(buttons=____)
)))
fig.show()