Python:Plotly .area()
The .area() function in Plotly Express creates an area chart by filling the space under a line plot, making it useful for visualizing trends and cumulative values over time. It supports grouped and stacked data, enabling easy comparison across categories.
Syntax
import plotly.express as px
fig = px.area(
data_frame,
x,
y,
color=None,
line_group=None,
markers=False,
text=None,
hover_name=None,
hover_data=None,
template=None
)
fig.show()
Parameters:
data_frame: The DataFrame containing the data. Ifdata_frameis not provided,xandymust be passed as separate lists.x: Ifdata_frameis given,xshould be a column name indata_framerepresenting the x-axis values. Otherwise, it takes as input a list of values for the x-axis.y: Ifdata_frameis given,yshould be column name(s) indata_framerepresenting the y-axis values. Otherwise, it takes as input a list of values for the y-axis. If multiple column names are provided, stacked area plots are created.color(optional): Column name for grouping and coloring different areas.line_group(optional): Defines which lines share the same fill area.markers(default=False): Adds markers to data points if set toTrue.text(optional): Adds text labels to data points.hover_name(optional): Column used for hover labels.hover_data(optional): Additional columns to display in the hover tooltip.template(optional): Specifies a predefined Plotly template.
Examples
Basic Area Chart
The following example demonstrates how to create an area chart using Plotly Express. This example uses a sample dataset showing daily sales over a period of time.
import plotly.express as pximport pandas as pd# Sample datadata = {"Date": pd.date_range(start="2024-01-01", periods=10, freq='D'),"Sales": [100, 120, 150, 130, 170, 180, 200, 190, 210, 230]}df = pd.DataFrame(data)# Create area chartfig = px.area(df, x="Date", y="Sales", title="Daily Sales Trend")fig.show()
The output will be as follows:

The output image displays an area chart where the x-axis represents the dates and the y-axis represents sales figures. The area under the line is filled, showcasing the trend of increasing sales over time. Peaks and valleys in the data are clearly visible, making it easy to identify fluctuations in daily sales.
Stacked Area Chart
The following example demonstrates how to create a stacked area chart by grouping data using the color parameter.
import plotly.express as pximport pandas as pdimport numpy as np# Sample datadates = pd.date_range(start="2024-01-01", periods=10, freq='D')data = {"Date": np.tile(dates, 2), # Repeat the dates for two categories"Sales": [100, 120, 150, 130, 170, 180, 200, 190, 210, 230] + [80, 110, 140, 120, 160, 170, 190, 180, 200, 220],"Category": ["A"] * 10 + ["B"] * 10}df = pd.DataFrame(data)# Create stacked area chartfig = px.area(df, x="Date", y="Sales", color="Category", title="Stacked Area Chart")fig.show()
The output will be as follows:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to clean, analyze, and visualize data with Python and SQL.
- Includes 15 Courses
- With Certificate
- Beginner Friendly.55 hours