in my data visualization series. See the following:
It’s time to start building your own data visualizations. In this article, I will walk through the process of visualizing time-series data in Python in detail. If you have not read the previous articles in my data visualization series, I strongly recommend reading at least the previous article for a review of Python.
Over the course of coding visualizations in Python, I will focus on three Python packages: Matplotlib, Plotly, and Altair. One approach to learning these might involve writing 1-2 articles per package, each one delving into the chosen package in detail. While this is a valid approach, the focus of my series is not on any particular library; it is about the data visualization process itself. These packages are simply tools—a means to an end.
As a result, I will structure this article and the ones to follow each around a particular type of data visualization, and I will discuss how to implement that visualization in each of the listed packages to ensure you have a breadth of approaches available to you.
First up: a definition for time-series data.
What Is Time-Series Data?
Formally, time-series data involves a variable that is a function of time. In simple terms, this just means some data that changes over time.
For example, a public company’s stock price over the last ten years is time-series data. If you’d prefer a more scientific example, consider the weather. A graph depicting the daily temperature of your favorite city over the course of the year is a graph that depicts time-series data.
Time-series data is an excellent starting point for data visualization for a few reasons:
- It is an extremely common and useful type of data. There is quite a bit of information that is dependent on time, and understanding it provides meaningful insight into the subject of interest going forward.
- There are tried and true methods to visualize time-series data effectively, as you’ll see below. Master these, and you’ll be in good shape.
- As compared with some other types of data, time-series visualizations are fairly intuitive to humans and align with our perception of time. This makes it easier to focus on the basic elements of visualization design when starting out, instead of getting bogged down in trying to make sense of very complex data.
Let’s start by taking a look at different visualization methods on a conceptual level.
How Is Time-Series Data Visualized?
The standard for time-series visualization is the famed line chart:

This chart generally puts time on the x-axis, and the variable that changes with time on the y-axis. This provides a view that appear to be “moving forward,” in line with humans’ linear perception of time.
Though the line chart is the standard, there are other, related possibilities.
Multiple Line Chart
This approach is a direct extension of a singular line chart and displays several related time series on the same plot, allowing comparison between groups or categories (e.g., sales by region):

Area Chart
Functionally, an area chart is almost exactly the same as a line chart, but the area under the line is filled in. It emphasizes the magnitude of change:

Stacked Area Chart
Technically, the stacked area chart is the analogue to the multiple line chart, but it is a bit trickier to read. In particular, the total is cumulative, with the baseline for each stacked line starting at the one below it. For instance, at 2023 in the chart below, “Ages 25-64” represents about 4 billion people, since we start counting where “Ages 15-24” ends.

Bar Chart (Vertical or Horizontal)
Finally, in some cases, a bar chart is also appropriate for time-series visualization. This approach is useful if you wish to show discrete time intervals—such as monthly sum or yearly average of some metric—rather than continuous data. That said, I will not be coding bar charts in this article.

Now, let’s get to actually building these visualizations. In each of the examples below, I will walk through the code in a specific visualization library for constructing line charts and area charts. I have linked the data here and encourage you to follow along. To internalize these techniques, you must practice using them yourself.
Coding Time-Series Visualizations in Matplotlib
import pandas as pd
import matplotlib.pyplot as plt
# Load data
df = pd.read_csv('sales_data.csv')
df['Date'] = pd.to_datetime(df['Date'])
# Example 1: Simple Line Chart
fig1, ax1 = plt.subplots(figsize=(10, 6))
ax1.plot(df['Date'], df['Product A Sales'], linewidth=2)
ax1.set_xlabel('Date')
ax1.set_ylabel('Sales')
ax1.set_title('Product A Sales Over Time')
ax1.grid(True, alpha=0.3)
plt.tight_layout()
# Display with: fig1
# Example 2: Multiple Line Chart
fig2, ax2 = plt.subplots(figsize=(10, 6))
ax2.plot(df['Date'], df['Product A Sales'], label='Product A', linewidth=2)
ax2.plot(df['Date'], df['Product B Sales'], label='Product B', linewidth=2)
ax2.plot(df['Date'], df['Product C Sales'], label='Product C', linewidth=2)
ax2.set_xlabel('Date')
ax2.set_ylabel('Sales')
ax2.set_title('Sales Comparison - All Products')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
# Display with: fig2
# Example 3: Area Chart
fig3, ax3 = plt.subplots(figsize=(10, 6))
ax3.fill_between(df['Date'], df['Product A Sales'], alpha=0.4)
ax3.plot(df['Date'], df['Product A Sales'], linewidth=2)
ax3.set_xlabel('Date')
ax3.set_ylabel('Sales')
ax3.set_title('Product A Sales - Area Chart')
ax3.grid(True, alpha=0.3)
plt.tight_layout()
# Display with: fig3
# Example 4: Stacked Area Chart
fig4, ax4 = plt.subplots(figsize=(10, 6))
ax4.stackplot(df['Date'], df['Product A Sales'], df['Product B Sales'], df['Product C Sales'],
labels=['Product A', 'Product B', 'Product C'], alpha=0.7)
ax4.set_xlabel('Date')
ax4.set_ylabel('Sales')
ax4.set_title('Total Sales - Stacked Area Chart')
ax4.legend(loc='upper left')
ax4.grid(True, alpha=0.3)
plt.tight_layout()
# Display with: fig4 Running this code produces the following four visualizations:




Let’s break the code down step by step to ensure you understand what is happening:
- First, we load the data into
pandasas a CSV file and ensure the date is properly represented as adatetimeobject. - Matplotlib structures charts within the
Figureobject, which represents the entire Canvas. This can be accessed directly usingplt.figure, but having multiple variables usingplt.subplotsis more intuitive for multiple visualizations. Every call toplt.subplotsdefines a new, separateFigure(canvas). - The line
fig1, ax1 = plt.subplots(figsize=(10, 6))defines the first subplot;fig1represents the canvas, butax1represents the actual plotting area within it and is the variable where you will make most changes. - Matplotlib has different functions for different charts. The
plotfunction plots 2-D points and then connects them to construct a line chart. This is what we specify in the lineax1.plot(df['Date'], df['Product A Sales'], linewidth=2). - The remaining lines are primarily aesthetic functions that do exactly what their names suggest: labeling axes, adding gridlines, and specifying layout.
- For the multiple line chart, the code is precisely the same, except we call
plotthree times: one for each set of x-y points that we want to graph to show all the products. - The area chart is almost identical to the line chart, except for the addition of
ax3.fill_between(df['Date'], df['Product A Sales'], alpha=0.4),which tells Matplotlib to shade the area below the line. - The stacked area chart, by contrast, requires us to use the
stacked_plotfunction, which takes in all three data arrays we want to plot at once. The remaining aesthetic code, however, is the same.
Try programming these yourself in your favorite IDE or in a Jupyter notebook. What patterns do you see? Which chart do you prefer the most?
Also, remember that you do not need to memorize this syntax, especially if you are new to programming data visualizations or new to Python in general. Focus on trying to understand what is happening on a conceptual level; you can always look up the particular syntax and plug your data in as needed.
This will hold true for the remaining two examples as well.
Coding Time-Series Visualizations in Plotly
Here is the code to generate the same visualizations as above, this time in Plotly’s style:
import pandas as pd
import plotly.graph_objects as go
# Load data
df = pd.read_csv('sales_data.csv')
df['Date'] = pd.to_datetime(df['Date'])
# Example 1: Simple Line Chart
fig1 = go.Figure()
fig1.add_trace(go.Scatter(x=df['Date'], y=df['Product A Sales'], mode='lines', name='Product A'))
fig1.update_layout(
title='Product A Sales Over Time',
xaxis_title='Date',
yaxis_title='Sales',
template='plotly_white'
)
# Display with: fig1
# Example 2: Multiple Line Chart
fig2 = go.Figure()
fig2.add_trace(go.Scatter(x=df['Date'], y=df['Product A Sales'], mode='lines', name='Product A'))
fig2.add_trace(go.Scatter(x=df['Date'], y=df['Product B Sales'], mode='lines', name='Product B'))
fig2.add_trace(go.Scatter(x=df['Date'], y=df['Product C Sales'], mode='lines', name='Product C'))
fig2.update_layout(
title='Sales Comparison - All Products',
xaxis_title='Date',
yaxis_title='Sales',
template='plotly_white'
)
# Display with: fig2
# Example 3: Area Chart
fig3 = go.Figure()
fig3.add_trace(go.Scatter(
x=df['Date'], y=df['Product A Sales'],
mode='lines',
name='Product A',
fill='tozeroy'
))
fig3.update_layout(
title='Product A Sales - Area Chart',
xaxis_title='Date',
yaxis_title='Sales',
template='plotly_white'
)
# Display with: fig3
# Example 4: Stacked Area Chart
fig4 = go.Figure()
fig4.add_trace(go.Scatter(
x=df['Date'], y=df['Product A Sales'],
mode='lines',
name='Product A',
stackgroup='one'
))
fig4.add_trace(go.Scatter(
x=df['Date'], y=df['Product B Sales'],
mode='lines',
name='Product B',
stackgroup='one'
))
fig4.add_trace(go.Scatter(
x=df['Date'], y=df['Product C Sales'],
mode='lines',
name='Product C',
stackgroup='one'
))
fig4.update_layout(
title='Total Sales - Stacked Area Chart',
xaxis_title='Date',
yaxis_title='Sales',
template='plotly_white'
)
# Display with: fig4 We obtain the following four visualizations:




Here is a breakdown of the code:
- Plotly is fully independent of Matplotlib. It uses similarly named
Figureobjects, but does not have anyaxobjects. - The
Scatterfunction withmode“lines” is used to build a line chart with the specified x- and y-axis data. You can think of theadd_tracefunction as adding a new component to an existingFigure. Thus, for the multiple line chart, we simply calladd_tracewith the appropriateScatterinputs three times. - For labeling and aesthetics in Plotly, use the
update_layoutfunction. - The area chart is built identically to the line chart, with the addition of the optional argument
fill='tozeroy'.- Upon first glance, this may look like some obscure color, but it is actually saying “TO ZERO Y,” specifying to Plotly the area that should be filled in.
- If you’re having trouble visualizing this, try changing the argument to “tozerox” and see what happens.
- For the stacked area chart, we need a different optional parameter:
stackgroup='one'. Adding this to each of theScattercalls tells Plotly that they are all to be constructed as part of the same stack.
An advantage of Plotly is that by default, all Plotly charts are interactive and come with the ability to zoom, hover for tooltips, and toggle the legend. (Note the images above are saved as PNGs, so you will need to generate the plots yourself in order to see this.)
Coding Time-Series Visualizations in Altair
Let’s finish off by generating these four visualizations in Altair. Here is the code:
import pandas as pd
import altair as alt
# Load data
df = pd.read_csv('sales_data.csv')
df['Date'] = pd.to_datetime(df['Date'])
# Example 1: Simple Line Chart
chart1 = alt.Chart(df).mark_line().encode(
x='Date:T',
y='Product A Sales:Q'
).properties(
title='Product A Sales Over Time',
width=700,
height=400
)
# Display with: chart1
# Example 2: Multiple Line Chart
# Reshape data for Altair
df_melted = df.melt(id_vars='Date', var_name='product', value_name='sales')
chart2 = alt.Chart(df_melted).mark_line().encode(
x='Date:T',
y='sales:Q',
color='product:N'
).properties(
title='Sales Comparison - All Products',
width=700,
height=400
)
# Display with: chart2
# Example 3: Area Chart
chart3 = alt.Chart(df).mark_area(opacity=0.7).encode(
x='Date:T',
y='Product A Sales:Q'
).properties(
title='Product A Sales - Area Chart',
width=700,
height=400
)
# Display with: chart3
# Example 4: Stacked Area Chart
chart4 = alt.Chart(df_melted).mark_area(opacity=0.7).encode(
x='Date:T',
y='sales:Q',
color='product:N'
).properties(
title='Total Sales - Stacked Area Chart',
width=700,
height=400
)
# Display with: chart4 We obtain the following charts:




Let’s break down the code:
- Altair has a slightly different structure from Matplotlib and Plotly. It takes some practice to grasp, but once you understand it, its intuitiveness makes building new visualizations straightforward.
- Everything in Altair revolves around the
Chartobject, into which you pass in your data. Then, you use amark_function to specify what kind of chart you want to build, and theencodingfunction to specify what variables will correspond to what visual elements on the chart (e.g., x-axis, y-axis, color, size, etc.). - For the line chart, we use the
mark_linefunction, and then specify that we want the date on the x-axis and the sales on the y-axis. - The
meltfunction does not change the data itself, just its structure. It puts the products all into a single column, a “long format” which is more amenable to Altair’s visualization model. For more details, check out this helpful article. - Once we transform the data as above, we can build our multiple line chart simply by adding a “color” encoding, as shown in the code. This was made possible because all the product types are now available in a single column, and we can tell Altair to distinguish them by color.
- The code for generating area charts showcases the beauty of Altair’s structure. Everything remains the same—all you need to do is change the function being used to
mark_area!
As you explore other types of visualizations on your own (and in future articles!), Altair’s model for building visualizations will become easier to implement (and hopefully appreciate).
What’s Next?
In future articles, I will cover how to use these libraries to build additional types of visualizations. As you continue learning, remember that the purpose of these articles is not to master any one tool. This is about learning data visualization holistically, and my hope is that you have walked away from this article with a better understanding of how time-series data is visualized.
As for the code, that comfort comes with time and practice. For now, you should feel free to take the examples above and adjust them for your own data as needed.
Until next time.
References
Source link
#Data #Visualization #Explained #Part #Visualizing #TimeSeries #Data #Python #Matplotlib #Plotly #Altair









