Area Plot
This module provides functionality for creating area plots from pandas DataFrames.
It is designed to visualize data distributions over time or across categories using filled area charts. These plots help highlight trends and comparisons between different groups by stacking or overlaying areas.
While this module supports datetime values on the x-axis, the plots.time_area module is better suited for explicitly time-based visualizations, offering features like resampling and time-based aggregation.
Core Features
- Flexible X-Axis Handling: Uses an index or a specified x-axis column (
x_col
) for plotting. - Multiple Area Support: Allows plotting multiple columns (
value_col
) or groups (group_col
). - Dynamic Color Mapping: Automatically selects a colormap based on the number of groups.
- Legend Customization: Supports custom legend titles and the option to move the legend outside the plot.
- Source Text: Provides an option to add source attribution to the plot.
Use Cases
- Time Series Visualization: Show trends in a metric over time (e.g., revenue by month).
- Stacked Area Charts: Compare contributions of different groups over time.
- Category-Based Area Plots: Visualize distributions of data across categories.
Limitations and Warnings
- Handling of Datetime Data: If a datetime column is passed as
x_col
, a warning suggests using the plots.time_area module for better handling. - Pre-Aggregated Data Required: The module does not perform data aggregation; data should be pre-aggregated before being passed to the function.
plot(df, value_col, x_label=None, y_label=None, title=None, x_col=None, group_col=None, ax=None, source_text=None, legend_title=None, move_legend_outside=False, **kwargs)
Plots an area chart for the given value_col
over x_col
or index, with optional grouping by group_col
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe to plot. |
required |
value_col |
str or list of str
|
The column(s) to plot. |
required |
x_label |
str
|
The x-axis label. |
None
|
y_label |
str
|
The y-axis label. |
None
|
title |
str
|
The title of the plot. |
None
|
x_col |
str
|
The column to be used as the x-axis. If None, the index is used. |
None
|
group_col |
str
|
The column used to define different areas in the plot. |
None
|
legend_title |
str
|
The title of the legend. |
None
|
ax |
Axes
|
Matplotlib axes object to plot on. |
None
|
source_text |
str
|
The source text to add to the plot. |
None
|
move_legend_outside |
bool
|
Move the legend outside the plot. |
False
|
**kwargs |
dict[str, any]
|
Additional keyword arguments for Pandas' |
{}
|
Returns:
Name | Type | Description |
---|---|---|
SubplotBase |
SubplotBase
|
The matplotlib axes object. |
Raises:
Type | Description |
---|---|
ValueError
|
If |