Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Area Charts

DS 2023 | Communicating with Data

image.png

Overview

Area charts are essentially line graphs in which the area under the line is filled in with color.

By coloring in the area, the focus shifts from trends to the accumulation or composition of the value represented by the y axis.

There are two main types of area chart when they involve more than one trend line:

  1. Overlapping: Area charts with multiple lines to make a comparison between groups.

  2. Stacked: Area charts that show how a whole is divided into component parts.

Like bar charts and unlike line graphs, area charts should always have a zero baseline for the y axis.

In addition, unlike bar charts, area charts typically do not show negative values.

Use Cases

  • Tracking time-series data, for example monthly sales revenue, daily stock prices, or annual website traffic.

  • Visualizing part-to-whole relationships over time, for example budget allocations by department or energy consumption by source.

  • Highlighting running totals, for example tracking total donations raised during a campaign or total active users acquired over a year.

  • Comparing lower and upper boundaries, such as minimum and maximum values, for example daily temperature highs and lows or financial confidence intervals.

A simple example

In this example, we look at energy consumption by fuel type from 1870 to 2021.

<class 'pandas.DataFrame'>
RangeIndex: 152 entries, 1870 to 2021
Data columns (total 4 columns):
 #   Column                            Non-Null Count  Dtype  
---  ------                            --------------  -----  
 0   Coal                              152 non-null    float64
 1   Gas                               140 non-null    float64
 2   Oil                               152 non-null    float64
 3   Cement, Flaring & Other Industry  72 non-null     float64
dtypes: float64(4)
memory usage: 4.9 KB
Loading...

We alter the column names to simplify our visualization.

We also convert the index to a datetime data type.

Loading...

Upon inspection, we notice a lot of null values.

We can interpret these as true absences, not faulty data collection.

Loading...

Stacked

In the chart below, the four lines and their areas are stacked on top of each other.

The chart shows both the overall growth of energy consumption and the relative contribution of each fuel.

You can see, for example, that before around 1945, coal is the dominant source of energy. After that, oil consumption grows to rival it.

<Figure size 640x480 with 1 Axes>

Unstacked

If we set that stacked for False, what we see if the relative growth of each fuel type, but not the overall growth.

In this chart, we can see more clearly that although oil consumption grows rapidly after WWII, it still does not exceed the use of coal.

We can see, however, that from around the late 1960s to the turn of the century, gas use did exceed coal use.

<Figure size 640x480 with 1 Axes>

Note that the unstacked chart is similar to the basic line graph in not showing combined amounts.

<Figure size 640x480 with 1 Axes>

Stacked with normed data

Now, if we want to show the relative amounts of fuel consumption over time without regard to absolute values, we can normalize the data so that the value of consumption for each year sums to 1.

This shows rather dramatically how the use of other fuels grow relative to coal.

Loading...
<Figure size 640x480 with 1 Axes>

Notice the margin at the top of this plot.

This is because Matplotlib pads the data range on each axis by 5% so that lines and markers don’t sit flush against the spines.

To override this, you can grab the Axes object and change the value of the y axis limits.

<Figure size 640x480 with 1 Axes>

Or, you can just set the ylim argument in the plot method.

<Figure size 640x480 with 1 Axes>

Unstacked with normed data

We can also show the normed data without stacking.

Again we see that coal consumption declines as oil increases.

We can also see clearly the period in which oil exceeds coal.

<Figure size 640x480 with 1 Axes>

Aside: Comparison to pie charts

Since we are showing composition of energy use, where the domain is small, we might compare area charts to pie charts.

Here is how we might accomplish the changing composition of energy use over time with pie charts.

First, let’s group our date by decade and get the means for each energy type.

Loading...
<Figure size 1600x1600 with 16 Axes>

Difference charts

Let’s look more closely at the relationship between coal and oil after WWII.

A simple line graph shows the growth and competition between the two.

<Figure size 640x480 with 1 Axes>

This is a good candidate for a difference chart, a variant of the area chart.

A difference chart is an area chart where only the areas of curve overlap are shaded.

A good example of this is William Playfair’s famous trade-balance chart from his Commercial and Political Atlas published in 1786:

Source: Wikimedia Commons

Here is a more contemporary example, showing the projected growth and decline of population segments in the United States:

Source: Eco3min

Let’s try to reproduce this effect with our energy dataset using Pandas and Matplotlib.

One approach is to create and plot a new column that consists of the minimum values of coal and oil.

To create it, we can get the row-wise .min() on the two variables.

Loading...

Then, If we plot min and set its color to white, it will mask everything except the overlaps.

Here’s how the three lines look as a line graph:

<Figure size 640x480 with 1 Axes>

The min line overwrites the other two lines since it is drawn last.

Now let’s view them as an difference chart:

<Figure size 640x480 with 1 Axes>

Here we improve the way the bounding plot lines are colored.

<Figure size 640x480 with 1 Axes>

If you want to emulate the effect of the second example above, you can divide the data into two parts and plot the two with different styles.

We also add a point and line to emphasize the point of cross-over, although in this case the location is inexact.

Note that if you do this, you will need to specify the same colors for both plots. Otherwise, the Pandas and Matplotlib with pick new colors for the second plot.

<Figure size 640x480 with 1 Axes>

Using Matplotlib’s fill_between

Underneath the hood, Pandas creates area plots using Matplotlib’s fill_between function.

This function takes three arguments: an x axis series, and two y axis series.

It will then create two line plots with the difference between the two lines shaded in.

This is very close to a difference chart.

Let’s look at an example.

Here we plot coal and oil from the coal_and_oil dataframe used above.

<Figure size 640x480 with 1 Axes>

You can see that it’s pretty close to what we want in a difference chart.

To create a difference chart to match the requirements of the one created above, we can construct it in the following way.

<Figure size 640x480 with 1 Axes>

This is actually a more effective way of creating a difference chart than the method shown above, but it requires that you work with Matplotlib and PyPlot directly, and you don’t get a legend out of the box.

In fact, you can create area plots, too, by setting one of the y axis arguments to 0.

<Figure size 640x480 with 1 Axes>

If we wanted to work with PyPlot directly for area charts, though, we’d have to figure out how to create a stacked one.

Here is one way to achieve the stacked effect:

<Figure size 640x480 with 1 Axes>

So, we are back to resorting to clever tricks to get the result we want.

Caveats

Although each of these charts gives insight to energy consumption over time, it is important to convey to the reader just what they are looking at.

In particular, unless clearly labeled, it is easy to mistake a stacked area charted for an unstacked one.

In the plot below, the curves are stacked, but a reader might think that coal has the least usage since it is on the bottom.

Even if readers known that the plots are stacked, their eyes may deceive them into thinking that all of the fuel usages are always rising.

But we know that coal usage actually declines for something like a 40 year period.

<Figure size 640x480 with 1 Axes>

Another source of confusion occurs when an area chart of normalized data is presented.

In the plot below, consider how the relative proportion of coal usage decreases even though the absolute usage increases.

<Figure size 640x480 with 1 Axes>