×

Area Charts with Plotly

An area chart is a form of a line chart with the area between the horizontal axis and the line that connects data points filled with colour. They are used to communicate an overall trend without being concerned about showing exact values.

Area Chart Plolty

The vertical axis represents a quantitative variable while the horizontal axis is a timeline or a sequence of numerical intervals.

Data points are connected by line segments forming a polyline, and the area between the polyline and the horizontal axis is filled with colour or some type of shading.

Submodule in Plotly to create Area Chart

Plotly Graph objects
import plotly.graph_objects as go
  • plotly.graph_objects is a different methodology having – fig.add_trace() and a different trace go.Scatter() to create area plot.
  • Takes Attributes as: x = is the name of a column in data_frame representing the timeline while y = is the name of a column in data_frame representing a particular region.
  • And, The stackgroup attribute is used to add the y values of the different traces in the same group.

Types of Area Charts

Standard Area Chart or simply Area Chart

Standard area charts are particularly effective to show the evolution of a numerical variable over time. The idea is to emphasise the overall trend and the peaks and troughs of the line that connects the data points in the chart.

Implementation
import plotly.graph_objects as go
  • Data Set
X = ['A', 'B', 'C', 'D']
Y = [100, 200, 500, 673]

When using Plotly graph objects first we have to create a figure(i.e a canvas) in which we actually add the plot. For creating a figure we have to just call the Figure() – method of the graph_objects – submodule.

fig = go.Figure()
  • Create the Area Chart object
obj = go.Scatter(
    x = X,
    y = Y,
    stackgroup = 'one',
)

Add the plot object to the Figure(or Canvas) and show the plot via show() – method, using the object of the figure created.

fig.add_trace(obj)
fig.show()
Output
Standard Area Chart

Stacked Area Chart

A stacked area chart is the amplification of a basic area chart. The values of each group are displayed on top of each other. This is the best chart to be used to show the distribution of categories as parts of a whole area where the cumulative total is unnecessary.

Implementation
import plotly.graph_objects as go
import numpy as np

X = ['A', 'B', 'C', 'D']

fig = go.Figure()
  • Using NumPy to create the random data set.
numpy.random.randint(low, high=None, size=None, dtype=int)

Return random integers from low (inclusive) to high (exclusive).

  • First Area chart
area_one = go.Scatter(
    name = 'First Chart',
    x = X,
    y = np.random.randint(1, 100, 10),
    stackgroup = 'one',
)
  • Second Area Chart
area_two = go.Scatter(
    name = 'Second Chart',
    x = X,
    y = np.random.randint(101, 200, 10),
    stackgroup = 'one',
)
  • Add both area chart’s objects to the figure we had created. Show up the figure.
fig.add_trace(area_one)
fig.add_trace(area_two)
fig.show()
Output
Stacked Area Chart Plotly

Percent Stacked Area Chart

As its name suggests, it is a variation Stacked Area Chart where each area chart indicates the percentage of each part referred to the total of the category.

Implementation of Percent Stacked Area Chart is very similar to the normal Stacked Area Chart – We only need to make one small change: add groupnorm = ‘percent’ to the first add_trace().

import plotly.graph_objects as go
import numpy as np

X = ['A', 'B', 'C', 'D']

fig = go.Figure()

area_one = go.Scatter(
    name = 'First Chart',
    x = X,
    y = np.random.randint(1, 100, 10),
    stackgroup = 'one',
    groupnorm = 'percent' #Group Norm attribute
)
area_two = go.Scatter(
    name = 'Second Chart',
    x = X,
    y = np.random.randint(101, 200, 10),
    stackgroup = 'one',
)

area_three = go.Scatter(
    name = 'Third Chart',
    x = X,
    y = np.random.randint(50, 200, 10),
    stackgroup = 'one',
)

fig.add_trace(area_one)
fig.add_trace(area_two)
fig.add_trace(area_three)

fig.show()

Output

Percent Stacked Area Chart Plolty