Date Buttons on Line Charts
Learn to enhance the line charts by adding date buttons, allowing us to efficiently filter data for frequently used time periods and gain valuable insights.
We'll cover the following...
Our data
For this lesson, let’s use some data on the stock price of Alibaba. We can print out the top few rows to see what it looks like below:
Press + to interact
# Import librariesimport pandas as pdimport numpy as np# Import datasetsalibaba = pd.read_csv('/usr/local/csvfiles//alibaba.csv', parse_dates=['Date'])print(alibaba.head())
Let’s plot all of the data to ascertain the patterns in the data:
Press + to interact
# Import librariesimport pandas as pdimport numpy as npimport plotly.express as pximport plotly.graph_objects as gofrom plotly.subplots import make_subplots# Import datasetalibaba = pd.read_csv('/usr/local/csvfiles//alibaba.csv', parse_dates=['Date'])# Make traces and addtrace1 = go.Scatter(x=alibaba['Date'],y=alibaba['Open'],marker=dict(color='#7F00FF'),name='Opening Price',mode='lines')trace2 = go.Scatter(x=alibaba['Date'],y=alibaba['Close'],marker=dict(color='black'),name='Closing Price',mode='lines')fig = go.Figure(data=[trace1, trace2])# Format and showfig.update_layout(title=go.layout.Title(text='Alibaba Stock prices', x=0.5, xref='paper'))fig.show()
Now we could use pandas
and filter the data so that we only get the range we are interested in, but it turns out that we can filter by date effortlessly using Plotly functionality. This introduces us to the next important point which ...