Filling area chart using plotly in Python

Plotly is a powerful Python library for creating interactive visualizations. It supports various chart types including line plots, scatter plots, area charts, bar charts, histograms, and more. Area charts are particularly useful for showing trends over time and comparing proportions between different categories.

What is a Filled Area Chart?

A filled area chart displays quantitative data graphically with the area below the line filled with color. It's effective for showing trends over time and comparing multiple data series. The px.area() function creates these charts with automatic color coordination for different categories.

Built-in Datasets

Plotly provides several built-in datasets for practice and examples:

  • carshare Car sharing data

  • election Election results data

  • gapminder Global development statistics

  • iris Flower measurements dataset

  • tips Restaurant tip data

  • wind Wind speed and direction data

Key Parameters

x, y Column names from the DataFrame for x-axis and y-axis values.

color Column name used to assign different colors to data categories.

line_group Groups data points into separate lines/areas.

Example 1: Wind Dataset

Let's create an area chart using the wind dataset, plotting strength against frequency with direction as color ?

import plotly.express as px

# Load the wind dataset
df = px.data.wind()
print("Dataset shape:", df.shape)
print("\nFirst few rows:")
print(df.head())

# Create area chart
fig = px.area(df, x="strength", y="frequency", color="direction")
fig.update_layout(title="Wind Strength vs Frequency by Direction")
fig.show()
Dataset shape: (128, 3)

First few rows:
  direction  strength  frequency
0         N         0        0.5
1         N         1        0.6
2         N         2        0.5
3         N         3        0.4
4         N         4        0.4

Example 2: Life Expectancy Over Time

Using the gapminder dataset to show life expectancy trends by continent ?

import plotly.express as px

# Load gapminder dataset
df = px.data.gapminder()

# Create area chart
fig = px.area(df, x="year", y="lifeExp", color="continent")
fig.update_layout(title="Life Expectancy by Continent Over Time")
fig.show()

Example 3: Custom Data with Random Numbers

Creating an area chart with randomly generated data ?

import plotly.express as px
import numpy as np
import pandas as pd

# Set seed for reproducible results
np.random.seed(50)

# Generate random data
random_x = np.random.randint(1, 100, 50)
random_y = np.random.randint(1, 100, 50)

print("X values:", random_x[:10])  # Show first 10 values
print("Y values:", random_y[:10])

# Create DataFrame for better organization
data = pd.DataFrame({
    'x_values': sorted(random_x),
    'y_values': np.random.randint(1, 100, 50)
})

# Create area chart
fig = px.area(data, x='x_values', y='y_values')
fig.update_layout(title="Area Chart with Random Data")
fig.show()
X values: [49 97 12 34 95  5 71 71 23  6]
Y values: [27 72 52 29 25 92 47 63 14 72]

Stacked vs Unstacked Areas

Area charts can be displayed in two modes:

Type Description Best For
Stacked Areas stack on top of each other Showing total and part-to-whole relationships
Unstacked Areas overlap, showing individual trends Comparing individual category trends

Conclusion

Plotly's area charts are excellent for visualizing trends and proportions over continuous data. Use stacked areas for part-to-whole analysis and unstacked areas for comparing individual trends. The library's interactive features make these visualizations perfect for data exploration and presentation.

Updated on: 2026-03-27T13:18:46+05:30

592 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements