# Plotly Stacked Area Chart

This tutorial will discuss creating a stacked area chart using the stackgroup parameter of the scatter() function of Plotly.

## Plotly Stacked Area Plot

A stacked area chart contains more than one area of different data frames. We can plot more than one area on the same graph using the stackgroup() parameter of the scatter() function.

After creating a scatter plot, we can add another area using the add_trace() function, which adds data in the same figure.

For example, let’s use some random data to create a scatter plot and then add another area using the add_trace() function. See the code below.

import plotly.graph_objects as go

x = [1, 2, 3, 4]
plot = px.Figure(go.Scatter(name='Data One',x = x,
y = [100, 200, 500, 673],stackgroup='one'))

name = 'Data Two',
x = x,
y = [56, 123, 982, 213],
stackgroup='one'))

plot.show()

Output:

We can also change the normalization of the graph using the groupnorm argument and setting its value to percent for the sum of the stack group.

For example, let’s plot the above graph using normalized values. See the code below.

import plotly.graph_objects as go

x = [1, 2, 3, 4]
plot = px.Figure(go.Scatter(name='Data One',x = x,
y = [100, 200, 500, 673],stackgroup='one',groupnorm='percent'))

name = 'Data Two',
x = x,
y = [56, 123, 982, 213],
stackgroup='one'))

plot.show()

Output:

We can use the add_trace() function to add another area into the scatter plot after it is created. Still, we can also do that while creating the figure by saving the data inside a variable and then using that variable to plot the stacked area plot.

For example, let’s create the stacked area plot again. See the code below.

import plotly.graph_objects as go

x = [1, 2, 3, 4]
data=[go.Scatter(name='Data One',x = x,
y = [100, 200, 500, 673],stackgroup='one'),
go.Scatter(
name = 'Data Two',
x = x,
y = [56, 123, 982, 213],
stackgroup='one')]

plot = px.Figure(data)
plot.show()

Output:

Author: Ammar Ali

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.