5 minutes read

Another plot we're going to familiarize ourselves with is the area chart. It is based on the line chart. The main difference lies in the X-axis. In an area chart, the part between the X-axis and the line is filled with color. Area charts and line charts are good for visualizing data that change over time. In this topic, we will learn to create area charts with matplotlib.

Creating a simple area chart

As you remember from the introductory topic, the first step is always to import matplotlib to your code:

import matplotlib.pyplot as plt

You are ready to plot your first area chart! We'll do it with the help of plt.fill_between(x, y). As you can see, this function takes two arguments – two arrays of numeric values. Let's say you want to plot the number of carrots your hamster Bonnie chomps each month of the year. We create a variable called months that stores numbers from 1 to 12 and a carrots variable that contains a list of 12 values: carrots consumed in one month.

The X-axis values come first — first months, than carrots, not the other way around.

months = range(1, 13)
carrots = [14, 13, 10, 15, 17, 15, 15, 13, 12, 10, 14, 11]
plt.fill_between(months, carrots)

Here's what we get:

Create a simple area chart with the fill_between matplotlib function

The plot is already cool. But as usual, we can work on the clarity. What do the values on the X- and Y-axes represent? Couldn't it be better to have all numbers from 1 to 12 on the X-axis rather than 2, 4, 6, 8, 10, and 12? Let's address these issues.

Titles and colors

If you have experience with any other kinds of matplotlib plots before, you're familiar with the way titles and colors are set. If not – no stress; this section is for you!

Color is specified in the plt.fill_between() function. Use the color attribute and a str as its value. This list of named colors can help you. "Named" means you can type the color name as a value; for other colors, you need the RGB code.

plt.fill_between(months, carrots, color="darkorange")

The plot title and labels for both axes are on separate lines; use the following functions: plt.xlabel(), plt.ylabel(), plt.title(), and str as arguments.

plt.xlabel("Months")
plt.ylabel("Number of carrots")
plt.title("Bonnie's monthly carrot intake")

Here's what we get as a result:

Add a title and axis labels to an area chart

Having taken a look at the graph, we don't require additional explanations on what all these numbers mean. As we've mentioned, it'd be nice to change the numbers on the axes. Let's move on to that!

Changing the axes

To change the numbers on the axes, we need to use separate functions, just like for titles. These functions are plt.xticks() and plt.yticks() that take arrays of numeric values. We want to display the numbers from 1 to 12 on the X-axis (you may want to create a list or use range(1, 13) to save time) and numbers from 0 to 20 with a step of 5 on the Y-axis (range(0, 21, 5)):

plt.xticks(range(1, 13))
plt.yticks(range(0, 21, 5))

Now, our graph looks like this:

Change the display axes of an area chart

Much better, isn't it? You can see that your hamster had at least 10 carrots per month, and the average value was somewhere between 10 and 15. It's a good thing to know when you're planning your shopping list!

Stacked area chart

Imagine that you have two hamsters. Bonnie has a friend named Clyde. It infers two carrot datasets that you want to plot on the same graph to compare the data. When you plot several datasets on one area chart, it turns to a stacked area chart. You can use it to display big stacks of data and see how much each stacked group (in our case, each hamster) contributes to the total.

To create this type of area chart, let's refer to another function: plt.stackplot(x, y1, y2). Note that the X-axis data still comes first and is followed by your datasets (two or more). Take a look at the data plot of carrot consumption for Bonnie and Clyde:

months = range(1, 13)
bonnie_carrots = [14, 13, 10, 15, 17, 15, 15, 13, 12, 10, 14, 11]
clyde_carrots = [13, 17, 12, 11, 11, 10, 15, 14, 13, 12, 11, 15]

plt.stackplot(months, bonnie_carrots, clyde_carrots)
plt.xlabel("Months")
plt.ylabel("Number of carrots")
plt.title("Bonnie and Clyde's monthly carrot intake")
plt.xticks(range(1, 13))
plt.yticks(range(0, 31, 5))

We have changed the values for plt.yticks() to accommodate the new data. Let's have a look at the result:

Create a stacked area chart with matplotlib

You can see the difference between the two datasets, but which is Bonnie and which is Clyde? To clarify that, we need to add a legend. You can do it in two steps: first, add the labels argument to the plt.stackplot() function, and then add the plt.legend() function without arguments. You can change the colors in this kind of area chart too — just pass a list of str to the colors argument (note that it's colors, not color):

plt.stackplot(months, bonnie_carrots, clyde_carrots, labels=["Bonnie", "Clyde"], colors=["yellow", "orange"])
plt.legend()

Here's the resulting plot:

Create a stacked area chart with legend in matplotlib

Now, our data is presented clearly and concisely.

Filling the area between two lines

Sometimes, you want to represent only the difference between two datasets, not their cumulative total. To do it, you need to plot two separate lines using plt.plot(x, y) and add plt.fill_between(). In this example, we also add plt.grid() to add a grid in the background that facilitates interpretation:

plt.plot(months, bonnie_carrots, color="green", label="Bonnie")
plt.plot(months, clyde_carrots, color="purple", label="Clyde")
plt.fill_between(months, bonnie_carrots, clyde_carrots, color="darkorange")

plt.xlabel('Months')
plt.ylabel('Number of carrots')
plt.title("The difference in monthly carrot intake")
plt.xticks(range(1, 13))
plt.yticks(range(0, 21, 5))

plt.legend()
plt.grid()

This is our result:

Fill the area between two line plots with fill_between

We can see that Bonnie and Clyde had the same number of carrots in July, but Bonnie chomped more in April, May, and June.

Conclusion

In this topic, we've discussed how to create area charts, set their titles, change colors and values on both axes. You can utilize this for graphs with multiple datasets, both in the form of a stacked area chart or a color-filled area between two lines. There are advanced features that haven't been covered in this topic, of course. Once you master the basic theory, we offer you to study the official matplotlib documentation: the simple area chart and the stacked area chart.

Now, let's practice what you've learned!

15 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo