AREA
Excel Usage
=AREA(data, title, xlabel, ylabel, area_color, alpha, grid, legend)
data(list[list], required): Input data.title(str, optional, default: null): Chart title.xlabel(str, optional, default: null): Label for X-axis.ylabel(str, optional, default: null): Label for Y-axis.area_color(str, optional, default: null): Area color.alpha(float, optional, default: 0.5): Alpha transparency.grid(str, optional, default: “true”): Show grid lines.legend(str, optional, default: “false”): Show legend.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Examples
Example 1: Basic area chart
Inputs:
| data | |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 3 |
| 4 | 5 |
Excel formula:
=AREA({1,2;2,4;3,3;4,5})
Expected output:
"chart"
Example 2: Area chart with title and axis labels
Inputs:
| data | title | xlabel | ylabel | |
|---|---|---|---|---|
| 1 | 2 | Growth Over Time | Time | Value |
| 2 | 4 | |||
| 3 | 3 |
Excel formula:
=AREA({1,2;2,4;3,3}, "Growth Over Time", "Time", "Value")
Expected output:
"chart"
Example 3: Stacked area with multiple series
Inputs:
| data | legend | ||
|---|---|---|---|
| 1 | 2 | 3 | true |
| 2 | 4 | 5 | |
| 3 | 3 | 4 |
Excel formula:
=AREA({1,2,3;2,4,5;3,3,4}, "true")
Expected output:
"chart"
Example 4: Custom transparency and color
Inputs:
| data | area_color | alpha | |
|---|---|---|---|
| 1 | 2 | green | 0.7 |
| 2 | 4 | ||
| 3 | 3 |
Excel formula:
=AREA({1,2;2,4;3,3}, "green", 0.7)
Expected output:
"chart"
Python Code
import sys
import matplotlib
IS_PYODIDE = sys.platform == "emscripten"
if IS_PYODIDE:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import io
import base64
import numpy as np
def area(data, title=None, xlabel=None, ylabel=None, area_color=None, alpha=0.5, grid='true', legend='false'):
"""
Create a filled area chart from data.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.stackplot.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input data.
title (str, optional): Chart title. Default is None.
xlabel (str, optional): Label for X-axis. Default is None.
ylabel (str, optional): Label for Y-axis. Default is None.
area_color (str, optional): Area color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is None.
alpha (float, optional): Alpha transparency. Default is 0.5.
grid (str, optional): Show grid lines. Valid options: True, False. Default is 'true'.
legend (str, optional): Show legend. Valid options: True, False. Default is 'false'.
Returns:
object: Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
"""
try:
if not isinstance(data, list) or not data or not isinstance(data[0], list):
return "Error: Input data must be a 2D list."
# Convert to numpy array
try:
arr = np.array(data, dtype=float)
except Exception:
return "Error: Data must be numeric."
if arr.ndim != 2 or arr.shape[1] < 2:
return "Error: Data must have at least 2 columns (X, Y)."
# Extract X and Y series
x = arr[:, 0]
y_series = [arr[:, i] for i in range(1, arr.shape[1])]
# Create figure
fig, ax = plt.subplots(figsize=(6, 4))
# Plot area
if len(y_series) == 1:
# Single series - use fill_between
ax.fill_between(x, 0, y_series[0], alpha=alpha, color=area_color if area_color else None)
else:
# Multiple series - use stackplot
labels = [f"Series {i+1}" for i in range(len(y_series))]
ax.stackplot(x, *y_series, alpha=alpha, labels=labels)
# Set labels and title
if title:
ax.set_title(title)
if xlabel:
ax.set_xlabel(xlabel)
if ylabel:
ax.set_ylabel(ylabel)
# Grid and legend
if grid == "true":
ax.grid(True, alpha=0.3)
if legend == "true" and len(y_series) > 1:
ax.legend()
plt.tight_layout()
if IS_PYODIDE:
buf = io.BytesIO()
plt.savefig(buf, format='png')
plt.close(fig)
buf.seek(0)
img_bytes = buf.read()
img_b64 = base64.b64encode(img_bytes).decode('utf-8')
return f"data:image/png;base64,{img_b64}"
else:
return fig
except Exception as e:
return f"Error: {str(e)}"