.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_frame
is not provided,x
andy
must be passed as separate lists.x
: Ifdata_frame
is given,x
should be a column name indata_frame
representing the x-axis values. Otherwise, it takes as input a list of values for the x-axis.y
: Ifdata_frame
is given,y
should be column name(s) indata_frame
representing 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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly55 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours