An Explainer on Time-Series Graphs With Examples

An Explainer on Time-Series Graphs With Examples

Introduction: Time Series and Visualization

A time-series graph is a type of visualization of time-series data where the data points are arranged in a grid. One axis (usually x) represents the time index, and the other the value of what is being observed.

The power of time-series graphs is that trends, irregularities, and other data features become immediately apparent. You don’t need to scroll through the raw data to find them. Let’s use an example to illustrate this: in this case, time-series data is displayed as a table and graph.

Time-series data sample shown as a graph vs. a table
Time-series data sample shown as a graph vs. a table (source: author)

Suppose you were in a meeting, and this data was presented to you as the total yearly sales of your company between 2001-2020. Then, a team debate would emerge about when the company experienced a massive sales boom or a drop a few years later. If you only had access to the table, good luck figuring out which time intervals your team was referring to.

On the other hand, if they had shown you the time-series graph, you would instantly realize what everyone was talking about—the sales boom of 2010 and the drop of 2018. This is the power of visualization.

Keep reading to learn more about building and using a time-series graph for temporal or time-series data. But first, let’s recap some basic concepts.

What Is a Time-Series Graph, and When Do You Use It?

Let’s break this down by explaining first what graphs are and how they relate to plots and charts, terms often used interchangeably, even by professionals. Strictly speaking, a graph is a visual representation of numerical data that emphasizes the mathematical relationship between the data. Now a chart is any visualization that is intended to make information easier to understand. This means that all graphs are charts, but not all charts are graphs.

Venn diagram illustrating the most common relationship between charts and graphs
Venn diagram illustrating the most common relationship between charts and graphs (source: author)

For example, pie charts, flow charts, or Venn diagrams are charts but not graphs. Graphs such as the one at the beginning of the article are also charts (that one specifically is a line or run chart). Finally, we have plots, which are visualizations of statistical data points.

Simply speaking, if you have any shapes displaying information, it’s a chart. If you have lines in a grid (that can be shown or not), you have a graph. If you have points, it’s a plot.

Remember that the distinction given here is what we believe to be the most common usage in the community. However, within this terminology, there is no universal right or wrong, only different levels of common usage. So when discussing the topic with others, always ensure you understand the intended meaning if the context is unclear.

For time-series data, you’ll most likely need graphs for time-series analysis or forecasting, reserving the non-graph charts for presenting your conclusions. A time series graph is one in which the value observed is plotted in the y-direction against its time index on the x-axis.

We use a time series graph to show all the data features in one image. In other words, a time-series graph is helpful for the following purposes:

  • Time-series decomposition
  • Model building
  • Analysis
  • Forecasting
  • And more

Essentially, anything we want to do with time-series data comes (almost always) with a corresponding graph.

By now, you must have realized that everyone in the modern age interacts with time series daily, although sometimes unknowingly. Most of these interactions occur with a time-series graph. Whenever you open a stock market app, health app, banking app, or even a bargain-hunting app that tracks changes in the prices of a desired product, the data is shown to you as a graph.

Google Trends statistic for Mariah Carey’s song All I Want For Christmas Is You, a time series graph that became an internet meme (source)
Google Trends statistic for Mariah Carey’s song All I Want For Christmas Is You, a time-series graph that became an internet meme (source)

Time-series graphs are in ads and television shows. Some even become internet memes, so the question isn’t really when to use them but how.

Examples and How to Use Them

How to use time-series graphs depends entirely on your particular case and what you want to demonstrate or emphasize. Let’s review a few examples:

Line graph (or chart): the most commonly used graph type for representing time series data. In this graph, a straight line connects each data point (sometimes, the points won’t be visible but only implied by the kinks in the graph’s line).

Multi-line graph: a graph with several time series line graphs displayed in the same grid. It will help you visualize a direct comparison between the series. For example, check out the following graph, representing the time series of anti-diabetic drug sales in Australia between 1991 and 2008. The time series was split into 18 smaller lines, each with different colors representing one year.

Multi-line graph of anti-diabetic drug sales by month in Australia between 1991 and 2008 (source)
Multi-line graph of anti-diabetic drug sales by month in Australia between 1991 and 2008 (source)

With the data displayed in this particular fashion, one can immediately see three facts:

  1. The revenue declines from January to February every year.
  2. The sales revenue overall has been growing over the years.
  3. The sales dip from January to February has become more severe over the years.

The latter fact would have been difficult to notice had this been a single-line graph spanning the full-time period.

Smooth interpolation graph: a graph in which polynomial lines connect the data points so that the sections meet smoothly at each data point (for this to happen, polynomials up to order three are sufficient). Technically speaking, linear graphs are exceptional cases of interpolation graphs where the polynomials are restricted to order one (thus linear), and the smoothness requirement is dropped (since you cannot achieve it using only linear polynomials).

You can imagine how useful this can be when you want to emphasize the smooth nature of the data you are displaying even if, due to its sampling nature, for example, it may not be so apparent from a regular line graph. There is, however, one caveat to be mindful of. Everything stated applies only to the lines connecting two data points. Everything involving multiple data points and approximation is no longer data visualization but analysis and fitting.

Area graph: a line or multi-line graph variation where the area above or (more commonly) below is shaded to emphasize a change in ratio among the variables. The following figure shows a multi-line area graph in a stacked arrangement where each value is stacked on top of another. Since this is a market share dataset, all values always add up to 100 percent.

Market share of smartphone operating systems between 2011 and Q1 2017 represented by a stacked area graph (source)
Market share of smartphone operating systems between 2011 and Q1 2017 represented by a stacked area graph (source)

Dual-axis or multi-axis graph: as the name suggests, this type of chart has two (or more) y-axes to display multiple time series of different units and value ranges. The colored axes distinguish the scale each point corresponds to. Both multi-axis and multi-line graphs belong to a broader class of overlapping charts of multiple time series since they, well, overlap.

When dealing with time series, you will often need composite graphs that don’t directly represent the data but show certain aspects you’re interested in. Because of this, we will list one more graph type used in conjunction with the time series graphs already mentioned.

Scatter plot: a graph in which each entry is a point (drawn as a circle, triangle, cross, or something similar), and the points are not connected as the ordering is not what’s of interest. For example, say you have two time series—one of the electricity demand per half-hour and one of the outside temperature at those same time indexes. You can then match the electricity demand with the outside temperature and see the relationship between the two in a scatter plot, as shown below.

Electricity demand in Victoria, Australia, for the outside temperature (source)
Electricity demand in Victoria, Australia, for the outside temperature (source)

How to Make a Time-Series Graph With Different Tools

Creating a time-series graph is mostly elementary and can be done in almost any environment you use. Of course, this depends on how visually elaborate you’d like your graph to be. The core, however, is simple.

Time-series graphs with Google Docs/Excel

Possibly, the most straightforward way to create a time-series graph is by using a spreadsheet editor, like Excel or Google Sheets. All you have to do is arrange your data in the columns such that the first column is the time index and the second your variable (this ordering is unnecessary, but it makes it easier).

In subsequent columns, you can link more variables to the same time index. After that, select the cells you want to plot, go to Insert, and then Chart (if your first row contains the variables' names, it will even include them in the labels). Once you have your default graph (usually a line or multi-line graph), you can modify it by changing it to, e.g., an area graph or other mentioned above.

Here’s one we made using Google Sheets to represent the daily sales of two companies—A and B—depending on the time index "Date."

Time-series graph of the daily sales of company A and company B plotted using Google Sheets (source: author)
Time-series graph of the daily sales of company A and company B plotted using Google Sheets (source: author)

Time-series graphs with Python

There are many ways to plot in Python, so it mostly depends on your other requirements and setting. As with most programming languages, the various libraries/packages created by developers worldwide make it usable to several users. That way, you don’t have to build everything from scratch—you can make the most of the pre-built blocks others have made and collected in the form of libraries.

So the question becomes: is there a library for this, and which do I use? For plotting graphs and data, you need two things: you have to tell Python (or any other language) what the data is and tell it to draw the data in the way you want. Arguably, the easiest way to achieve this in Python for our above dataset is using the Pandas library to ingest the data and pyplot (from the matplotlib library) to then create the graph.

A minimal working example is shown below (with our data in a .csv file called data.csv).

import pandas as pd 
import matplotlib.pyplot as plt 
df = pd.read_csv('data.csv') 
df.plot(x='Date', y=[‘A','B']) 
Time-series graph of the daily sales of company A and company B plotted with Python using the code above (source: author)
Time-series graph of the daily sales of company A and company B plotted with Python using the code above (source: author)


New to Python? Start by taking a Python tutorial. Once you’re more proficient, check how to use Timescale with Python to manage and plot your time-series data.

Time-series graphs in R

Similarly to Python, there are many ways to plot in R. This language is special because it is specifically designed for statistical computing and graphics. Unsurprisingly, it has a whole package dedicated to time-series data and analysis, the TSstudio package. To get started, check out this tutorial on R.

Keep in mind that any kind of programming is best learned by trial and error. To give you a head start, take a look at our previous example. As in Python, we first have to bring in our data. However, we don’t need a special package this time—this is what R was designed for.

After importing the CSV file, we have to transform the data into a format the library understands. Since this is a time series with dates as the variable, we use as.Date() to tell R to treat it as such. Now, once everything is set up correctly, it’s simply a matter of using the ts_plot() function, and R takes care of the rest.

library(TSstudio) 
data <- read.csv(“data.csv") 
data$Date <- as.Date(data$Date, format=“%d %b %Y") 
ts_plot(data2) 
Time-series graph of the daily sales of company A and company B plotted with R using the code above (source: author)
Time-series graph of the daily sales of company A and company B plotted with R using the code above (source: author)

Excited to explore more opportunities using TSstudio? Check out this article on the full power of the TSstudio package.

What about SQL?

If you’re using a SQL database to store your time-series data, you have two options. You can export the data you want to visualize to a CSV file or similar data format, plot it using Python or R, or use a Python SQL library. This is not as straightforward as it seems, though.

There are different packages depending on the SQL database you use. For example, if you’re an SQLite user, you’ll need the sqlite3 library, but if you use MySQL, you’ll have to download mysql-connector-python. For PostgreSQL, reach out for a third-party driver, such as psycopg2.

To learn more about connecting your SQL database with Python, check out this introduction to Python SQL libraries.

However, if you use Timescale for your time series, this problem has become exponentially simpler because you can use it with Grafana. Check out our tutorial on how to plot time-series graphs with Grafana and Timescale.

Still interested? Read how you can use our hyperfunctions to accelerate your Grafana dashboards.

Want to Learn More?

This blog post is only a teaser of what you can find in the world of time-series graphs and their relation to time-series analytics.

But the best way to learn more is by trying it out! Find an interesting dataset and start digging. You’ll soon realize there isn’t much you can do without graphs, so plot away and explore all the ways to visualize data.

To do this, you will need a database capable of handling all those datasets. Why not try Timescale (free 30-day trial, no credit card required)? It’s fully cloud-native and allows you to scale quickly and efficiently without breaking the bank.

Read more about time series:

Ingest and query in milliseconds, even at terabyte scale.
This post was written by
10 min read
Timescale Cloud
Contributors

Related posts