How to Plot Timeseries Data in Python and Plotly

A simple tutorial on handling time series data in Python from extracting the dates and others to plotting them to charts.

Lia Ristiana
Nerd For Tech
6 min readFeb 28, 2021

--

Image by Burst from Pexels.com

Handling time series data can be a bit tricky. When I first had to deal with time-series data in Python and put them into charts, I was really frustrated. I probably spent a whole day just trying to figure out how to extract the dates or the months from a series of timestamp data, and at the end of the day, I still didn't understand a lot of things.

Then when I had to transform these data into charts using libraries like matplotlib or plotly, it only added more confusion to me. Luckily by now, I have finally learned how to solve these problems.

I am going to show you some code about this that I have learned recently. Hopefully, my future self or anyone looking for clues to do time series visualization will find this helpful.

The Data

I don't want to use dummy data for our examples here, so I am going to use real data instead. I collected all tweets from 1 January 2020 to 31 January 2021 (13 months of data) that had the word “malioboro” in it.

Malioboro is a street in Yogyakarta — the city I have been living in for the past two years — which is known as the largest tourist attraction in the city. It’s famous not only to local tourists but also to international tourists (at least before the COVID-19 pandemic this place used to be swarmed by tourists from countries around the world). Hence, I believed it must be mentioned quite significantly on Twitter on day to day basis.

I used a library called Twint to collect the data from Twitter. You can download the data here (JSON format) and take a look at the code I used to collect the data here on my Github page. However, you can use any data you own (as long as they have dates in them) to follow this tutorial.

People in Malioboro Street. (Photo by arialqadri — on Unsplash)

Loading the Data

Let's start by importing some important packages and the data themselves.

Since the data collected are in JSON format, I need to make Python read them line by line and convert them to pandas data frame format.

There are exactly 88,035 tweets collected.

For a year-long data, it’s pretty awesome, right? Malioboro is indeed a famous place, otherwise, Twitter wouldn't be talking about it.

Extracting Dates

The time data aren’t in a standard format yet. If you look at the picture below, you’ll see that it has the timezone name in the back. My laptop’s timezone is set to GMT+07 (Bangkok/Jakarta, South East Asia), and Twint probably followed that format.

Depending on the timezone set in your laptop, you’ll probably see similar cases. We are going to remove these substrings and leave only the date-time to be stored into a new created column.

Now we have the date-time data stored in the created column. However, since we only need the dates and months data, we are going to parse those things using the following code.

To plot the data we need the numbers of tweets per their respective time unit (months or days).

Plotting by Month

First, we are going to plot the data by month. We do that by making a new data frame consisting of each month and their respective numbers of tweets.

There are 13 months from Jan 2020 to Jan 2021.

These data are now ready to be plotted.

Plotting by Month in A Line Chart

Plotting by Month in A Bar Chart

Plotting by Day

You can also make a more detailed chart that shows the trend day by day, but I don't suggest this for a longer time data (say, three years or more) because the lines would become really confusing and harder to read.

We do this by creating a data frame that stores the number of tweets day by day.

This chart looks very interesting because now you can see the peaks on certain dates. There must be something important occurring during those days that caused Twitter to talk about Malioboro more than usual.

I am going to highlight these important dates by putting some dots to show their significance in the data.

Here I am going to get the top three peak dates and store them in a data frame.

Then I use this data frame to put notes on the chart. It’s pretty similar to the previous chart we have made, but it has some points that highlight these dates.

You can change the text shown on the points with any text you want, for example, you can use that to write down some notes about the events that caused the peak points.

These charts are interesting, but without a story to accompany them, the readers wouldn't get many clues.

In my case, Oct 4 and Oct 8 became the peak points in the chart because there’s a huge demonstration that happened in Malioboro during those dates. Twitter users were quick to update the situations which caused the term Malioboro to trend and became the talk of the news.

On Dec 31 another peak point happened because the local Governments decided not to lockdown or restrict Malioboro during New Year's Eve despite the increasing number of COVID-19 cases in Yogyakarta. Users on Twitter immediately joined in the controversies by tweeting their opinion.

These charts and context have hopefully created some kind of story about our data.

In short, plotting time series data using Plotly are actually pretty simple and straightforward. If you still find some things confusing, that's okay, you don't have to get everything on the first try because sometimes it takes some time to get used to.

Hopefully, you find this tutorial easy to follow, otherwise hit me up with some comments!

You can look at the whole code I used in this article on my Github Repository below. Thanks for reading!

--

--