Draw Time Series in plotly Graph in Python (4 Examples)

 

Hi! This tutorial will show you how to build a plotly time series plot in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Install & Import plotly

In order to draw a time series plot in plotly, we will need to, first of all, install and import plotly into our Python programming environment.

Therefore, in your Python programming IDE, run the lines of code below to install and import plotly:

# install plotly
pip install plotly
 
# import plotly
import plotly.express as px

With plotly installed and imported, we can now access its plot-building functions. But before we can start drawing our time series plot, we need data.
 

Create Example Dataset

We will make use of plotly’s built-in stock dataset in this tutorial. To load and preview the first 5 rows of the DataFrame, run the code below:

# load dataset
df = px.data.stocks()
 
# preview first 5 rows
print(df.head(5))
 
#         date      GOOG      AAPL      AMZN        FB      NFLX      MSFT
#0  2018-01-01  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000
#1  2018-01-08  1.018172  1.011943  1.061881  0.959968  1.053526  1.015988
#2  2018-01-15  1.032008  1.019771  1.053240  0.970243  1.049860  1.020524
#3  2018-01-22  1.066783  0.980057  1.140676  1.016858  1.307681  1.066561
#4  2018-01-29  1.008773  0.917143  1.163374  1.018357  1.273537  1.040708

With our dataset loaded, we can now draw time series graphs in plotly.
 

Example 1: Draw Time Series Line Plot

In this first example, we will build a plotly time series line chart. Run the code below to do so:

fig = px.line(df, x="date", y="NFLX")
 
fig.show()

In just two lines of code, we have plotted a time series of Netflix’ stock performance.

All we had to do was to pass the DataFrame to the px.line() method and define the X axis as date and the Y axis as NFLX to plot the time series.

Let us see other time series graphs we can plot using the same data.

 

Example 2: Draw Time Series Bar Plot

In this second example, we will visualize the data as a time series bar plot like so:

fig = px.bar(df, x = "date", y = "NFLX")
 
fig.show()

As you can see, the code is very similar to the one we used to draw the line chart in the previous example. The only difference here is that we used the px.bar() method instead of px.line() to draw the time series.

So, the same data can also be visualized as a bar plot and not just as a line plot.
 

Example 3: Draw Time Series Scatter Plot

In this third example, we will make our time series plot a scatter plot:

fig = px.scatter(df, x = "date", y = "NFLX")
 
fig.show()

Here, we have visualized the data as a time series scatter plot using the px.scatter() method.

This is another way to visualize a time series in plotly.

 

Example 4: Draw Facet Time Series Plot

In this final example, we will build a facet time series area plot to visualize the data of all the companies in the dataset.

To do that, we will make a small modification to the DataFrame:

df = px.data.stocks(indexed=True)
 
fig = px.area(df, facet_col="company", facet_col_wrap=2)
 
fig.show()

Here, setting indexed = True in the px.data.stocks() dataset method changes the date column to a column named “company” with which we are able to simultaneously plot the data of all the companies in the dataset.

Then, in px.area() method, we set the facet_col = argument to company and the facet_col_wrap = to 2, which creates a grid of 2 columns.

You can experiment with other plot types and see other interesting ways to visualize time series in plotly in Python.
 

Video, Further Resources & Summary

Do you need more explanations on how to draw a plotly time series plot in Python? Then, you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to draw a plotly time series plot in Python.

 

The YouTube video will be added soon.

 

You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:

This post has shown how to draw a plotly time series plot in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top