# Customized Stacked Area Plot using Python

## What is Stacked Area Plot?

Stacked area charts are extension of area charts which evaluates multiple groups in single chart. An area graph represents change in quantities of one or more groups over time. Using `stackplot()` function of matplotlib library to plot customized stacked area plot in python.

In this article, I will explain you about how to draw customized stacked area plot in python.

## Using Parameters for Customizing Area Chart

Parameters using matplotlib.pyplot to customize stacked area plot are given below

```Parameters
----------
x : 1d array of dimension N

y : 2d array (dimension MxN), or sequence of 1d arrays (each dimension 1xN)

The data is assumed to be unstacked. Each of the following
calls is legal::

stackplot(x, y)               # where y is MxN
stackplot(x, y1, y2, y3, y4)  # where y1, y2, y3, y4, are all 1xNm

baseline : {'zero', 'sym', 'wiggle', 'weighted_wiggle'}
Method used to calculate the baseline:

- ``'zero'``: Constant zero baseline, i.e. a simple stacked plot.
- ``'sym'``:  Symmetric around zero and is sometimes called
'ThemeRiver'.
- ``'wiggle'``: Minimizes the sum of the squared slopes.
- ``'weighted_wiggle'``: Does the same but weights to account for
size of each layer. It is also called 'Streamgraph'-layout. More
details can be found at http://leebyron.com/streamgraph/.

labels : Length N sequence of strings
Labels to assign to each data series.

colors : Length N sequence of colors
A list or tuple of colors. These will be cycled through and used to
colour the stacked areas.

**kwargs
All other keyword arguments are passed to `Axes.fill_between()`.
```

Parameters using pandas to customize area plot

```Parameters
----------
x : label or position, optional
Coordinates for the X axis. By default uses the index.
y : label or position, optional
Column to plot. By default uses all columns.
stacked : bool, default True
Area plots are stacked by default. Set to False to create a
unstacked plot.
**kwargs
Additional keyword arguments are documented in
:meth:`DataFrame.plot`.```

## Installation of Packages

We will need numpy, matplotlib and seaborn packages to plot customized stacked area chart in python. If you don’t have these packages installed on your system, install it using below commands.

```pip install numpy
pip install matplotlib
pip install seaborn```

## Customize Stacked Area Plot in Python

Let’s see an example to plot customize stacked area chart using seaborn color palette.

### Installation of Packages

We will need numpy, matplotlib and seaborn packages to plot customized stacked area graph. Install packages using below command.

```pip install numpy
pip install matplotlib
pip install seaborn```

### Import libraries

Import numpy, matplotlib and seaborn libraries in our python code to get started with plotting stacked area graph.

```import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns```

Cool Tip: Learn How to plot bubble chart in python !

### Prepare dataset

We will need x and y axis dataset values to plot stacked area chart.

Lets create dataset for x using Numpy arange() to get array object evenly spaces values within given range and  for the 5 values of x we have assigned list of three list of 5 values representing three separate groups in y variable.

```# Create dataset for X and Y axis
x=np.arange(1,6)
y=[ [1,4,3,5,9], [2,3,9,6,10], [2,8,10,8,12] ]```

### Plot Stacked Area chart using stackplot()

We declare a variable pal as set1 color palette from seaborn. Using  matplotlib.pyplot `stackplot()` function to plot customized stacked area plot in python.

The values of x and y dataset are assigned in stackplot(). We have assigned a list to lables parameter, assigned pal parameter to colors and alpha as 0.4 for transparency.

We have assigned the location of legends as upper left in plot.

```pal = sns.color_palette("Set1")
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4)
plt.legend(loc='upper left')```

Cool Tip: Learn How to plot Treemap chart in python !

### Show Stacked Area Chart

Using  Matplotlib show() function to show the graphical view of area chart.

`plt.show()`

### Stacked Area plot Python Code

Use below customized stacked area plot in python using Matplotlib source code

```# library
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Create dataset for X and Y axis
x=np.arange(1,6)
y=[ [1,4,3,5,9], [2,3,9,6,10], [2,8,10,8,12] ]

# use a known color palette
pal = sns.color_palette("Set1")
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4 )
plt.legend(loc='upper right')
plt.show()```

## Customized Stacked Area plot in Python

Let’s see an example to customize stacked area chart using own color palette.

### Installation of Packages

We will require numpy and matplotlib packages installed on our system to plot stacked area plot. If we don’t have packages installed, we can install packages with the command below.

```pip install numpy
pip install matplotlib ```

### Import libraries

We will import numpy and matplotlib libraries to plot stacked area plot. We have imported the numpy library as np and matplotlib.pyplot library as plt to reduce the complexity of code.

```import numpy as np
import matplotlib.pyplot as plt```

Cool Tip: Learn How to plot Bubble Chart in python !

### Prepare dataset

We have created variables x and y as datasets for stacked area plot using matplotlib stackplot() function.

Let’s create dataset for x using Numpy `arange()` to get array object evenly spaces values within given range and Y dataset having three list of 5 values.

```# Create dataset for X and y axis
x=np.arange(1,6)
y=[ [1,4,3,5,9], [2,3,9,6,10], [2,8,10,8,12] ]```

### Plot Stacked Area chart

We will be creating our own color list to create palette and used use it in stacked area chart.

The values of x and y dataset are assigned in stackeplot() function we have assigned color of plot as variable pal having palette and alpha as 0.4 for transparency.

We have assigned the location of legends as upper left in plot.

```pal = ["#9b59b6", "#e74c3c", "#34495e", "#2ecc71"]
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4)
plt.legend(loc='upper left')```

Stacked Area Plot Python Source Code

Use below entire source code to plot stacked area plot in python using Matplotlib.

```# import libraries
import numpy as np
import matplotlib.pyplot as plt

# Create dataset for  x and y axis

x=np.arange(1,6)
y=[ [1,4,3,5,9], [2,3,9,6,10], [2,8,10,8,12] ]