How do you plot area on a graph in python?


An area chart is really similar to a line chart, except that the area between the x axis and the line is filled in with color or shading. It represents the evolution of a numeric variable. This section starts by considering matplotlib and seaborn as tools to build area charts. It then shows a few other options for timeseries.

⏱ Quick start

There are 2 main ways to build an area chart with Matplotlib. In both case it requires 2 numeric vectors of values as input.

  • The fill_between() function
  • The stackplot() function that is more useful for stacked area charts

# library
import numpy as np
import matplotlib.pyplot as plt

# Create data
x=range(1,6)
y=[1,4,6,8,4]

# Area plot
plt.fill_between(x, y)
plot.show()

Area chart with Matplotlib

Matplotlib is a great fit to build an area chart thanks to its fill_between() function. Here are a few examples explaining its basics and how to apply some common customization.

Area chart with Seaborn

Seaborn is another great alternative to build an area chart with python. The below examples show how to start basic, apply usual customization, and use the small multiple technique for when you have several groups to compare.

From the web

The web is full of astonishing charts made by awesome bloggers, (often using R). The Python graph gallery tries to display (or translate from R) some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!


A stacked area chart displays the evolution of a numeric variable for several groups of a dataset. Each group is displayed on top of each other, making it easy to read the evolution of the total, but hard to read each group value accurately. In python, stacked area charts are mainly done thanks to the stackplot() function

⏱ Quick start

Here is a quick start code snippet to demo how the stackplot() function of matplotlib works.

Note that here each groups are provided in its own vector of values. The basic stacked area blog post explains how to use the function from any type of data format.

# library
import numpy as np
import matplotlib.pyplot as plt

# Create data
x=range(1,6)
y1=[1,4,6,8,9]
y2=[2,2,7,10,12]
y3=[2,8,5,10,6]

# Basic stacked area chart.
plt.stackplot(x,y1, y2, y3, labels=['A','B','C'])
plt.legend(loc='upper left')

⚠️ The issue with stacking

Stacked area charts must be used with care since they suffer a number of caveats. They are appropriate to study the evolution of the whole and the relative proportions of each group, but not to study the evolution of each individual group.

For instance, it is pretty hard to understand how the green group evolves on the chart below. Can you spot if its value is increasing, decreasing or stable?

Stacked Area chart with Matplotlib

Matplotlib is the most common way to build a stacked area chart with Python. The examples below start by explaining to basics of the stackplot() function. The also describe the most common type of customization like changing colors, controling group order and more.

💡 The baseline parameter

It is important to note that the stackplot() function of matplotlib has abaseline parameter. This parameter controls how groups are displayed around the x axis, what allows to mimic a streamgraph.

Percent Stacked Area chart with Matplotlib

A variation of the stacked area graph is the percent stacked area graph where the value of every groups are normalized at each time stamp. It allows to study the percentage of each group in the whole more efficiently.

Fortunately, the pandas library has a divide() function that allows to apply this normalization easily.

Stacked Area chart with Pandas

Pandas is mainly useful to normalize your dataset and build a stacked area chart. Surprisingly, it also provides a plot.area()that can be handy to build a stacked area chart.

From the web

The web is full of astonishing charts made by awesome bloggers, (often using R). The Python graph gallery tries to display (or translate from R) some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!

DataFrame.plot.area(x=None, y=None, **kwargs)[source]#

Draw a stacked area plot.

An area plot displays quantitative data visually. This function wraps the matplotlib area function.

Parametersxlabel or position, optional

Coordinates for the X axis. By default uses the index.

ylabel or position, optional

Column to plot. By default uses all columns.

stackedbool, default True

Area plots are stacked by default. Set to False to create a unstacked plot.

**kwargs

Additional keyword arguments are documented in DataFrame.plot().

Returnsmatplotlib.axes.Axes or numpy.ndarray

Area plot, or array of area plots if subplots is True.

See also

DataFrame.plot

Make plots of DataFrame using matplotlib / pylab.

Examples

Draw an area plot based on basic business metrics:

>>> df = pd.DataFrame({
...     'sales': [3, 2, 3, 9, 10, 6],
...     'signups': [5, 5, 6, 12, 14, 13],
...     'visits': [20, 42, 28, 62, 81, 50],
... }, index=pd.date_range(start='2018/01/01', end='2018/07/01',
...                        freq='M'))
>>> ax = df.plot.area()

How do you plot area on a graph in python?

Area plots are stacked by default. To produce an unstacked plot, pass stacked=False:

>>> ax = df.plot.area(stacked=False)

How do you plot area on a graph in python?

Draw an area plot for a single column:

>>> ax = df.plot.area(y='sales')

How do you plot area on a graph in python?

Draw with a different x:

>>> df = pd.DataFrame({
...     'sales': [3, 2, 3],
...     'visits': [20, 42, 28],
...     'day': [1, 2, 3],
... })
>>> ax = df.plot.area(x='day')

How do you plot area on a graph in python?

How do you plot area on a graph?

Area Graph Area Graphs are drawn by first plotting data points on a Cartesian coordinate grid, joining a line between the points and finally filling in the space below the completed line. Like Line Graphs, Area Graphs are used to display the development of quantitative values over an interval or time period.

How do you plot area under a curve in Python?

Set the figure size and adjust the padding between and around the subplots..
Ceate random data points, x, y1 and y2, using numpy..
To fill the area under the curve, put x and y with ste="pre", using fill_between() method..
Plot (x, y1) and (x, y2) lines using plot() method with drawstyle="steps" method..

How do we plot area chart using pandas?

Draw an area plot based on basic business metrics:.
In [1]: import numpy as np import pandas as pd..
In [3]: ax = df. plot. area(stacked=False).
In [4]: ax = df. plot. area(y='sales').
In [5]: df = pd. DataFrame({ 'sales': [4, 2, 4], 'visits': [30, 52, 38], 'day': [1, 2, 3], }) ax = df. plot. area(x='day').

How do you plot a graph in Python?

Following steps were followed:.
Define the x-axis and corresponding y-axis values as lists..
Plot them on canvas using . plot() function..
Give a name to x-axis and y-axis using . xlabel() and . ylabel() functions..
Give a title to your plot using . title() function..
Finally, to view your plot, we use . show() function..