AREA_3D

Excel Usage

=AREA_3D(data, title, xlabel, ylabel, zlabel, area_color, alpha)
  • data (list[list], required): Input data with 6 columns (X1, Y1, Z1, X2, Y2, Z2).
  • 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.
  • zlabel (str, optional, default: null): Label for Z-axis.
  • area_color (str, optional, default: null): Area color.
  • alpha (float, optional, default: 0.5): Alpha transparency.

Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).

Example 1: 3D double helix area

Inputs:

data title
1 0 0 -1 0 0 3D Helix Area
0.707 0.707 0.25 -0.707 -0.707 0.25
0 1 0.5 0 -1 0.5
-0.707 0.707 0.75 0.707 -0.707 0.75
-1 0 1 1 0 1

Excel formula:

=AREA_3D({1,0,0,-1,0,0;0.707,0.707,0.25,-0.707,-0.707,0.25;0,1,0.5,0,-1,0.5;-0.707,0.707,0.75,0.707,-0.707,0.75;-1,0,1,1,0,1}, "3D Helix Area")

Expected output:

"chart"

Example 2: Simple column data

Inputs:

data
0 0 0 1 1 0
0 1 1 1 0 1

Excel formula:

=AREA_3D({0,0,0,1,1,0;0,1,1,1,0,1})

Expected output:

"chart"

Example 3: Blue area with high transparency

Inputs:

data area_color alpha
0 0 0 1 1 1 blue 0.3
1 0 1 0 1 0

Excel formula:

=AREA_3D({0,0,0,1,1,1;1,0,1,0,1,0}, "blue", 0.3)

Expected output:

"chart"

Example 4: Green area with title

Inputs:

data area_color title
0 0 0 2 2 2 green Green 3D Area
2 0 2 0 2 0

Excel formula:

=AREA_3D({0,0,0,2,2,2;2,0,2,0,2,0}, "green", "Green 3D Area")

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
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import io
import base64
import numpy as np

def area_3d(data, title=None, xlabel=None, ylabel=None, zlabel=None, area_color=None, alpha=0.5):
    """
    Create a 3D filled area chart between two 3D lines.

    See: https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.fill_between.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): Input data with 6 columns (X1, Y1, Z1, X2, Y2, Z2).
        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.
        zlabel (str, optional): Label for Z-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.

    Returns:
        object: Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    try:
        data = to2d(data)
        if not isinstance(data, list) or not data or not isinstance(data[0], list):
            return "Error: Input data must be a 2D list."

        arr = np.array(data, dtype=float)
        if arr.shape[1] < 6:
            return "Error: Data must have at least 6 columns (X1, Y1, Z1, X2, Y2, Z2)."

        x1, y1, z1 = arr[:, 0], arr[:, 1], arr[:, 2]
        x2, y2, z2 = arr[:, 3], arr[:, 4], arr[:, 5]

        # Create figure with 3D projection
        fig, ax = plt.subplots(figsize=(10, 7), subplot_kw={"projection": "3d"})

        # Determine color and alpha
        c = area_color if area_color and area_color != "" else "C0"
        a = float(alpha) if alpha is not None else 0.5

        # Use Poly3DCollection for compatibility with Matplotlib < 3.10
        verts = []
        for i in range(len(x1) - 1):
            vtx = [
                (x1[i], y1[i], z1[i]),
                (x1[i+1], y1[i+1], z1[i+1]),
                (x2[i+1], y2[i+1], z2[i+1]),
                (x2[i], y2[i], z2[i])
            ]
            verts.append(vtx)

        poly = Poly3DCollection(verts, alpha=a, facecolor=c)
        ax.add_collection3d(poly)

        # Plot lines for clarity
        ax.plot(x1, y1, z1, linewidth=2, color=c)
        ax.plot(x2, y2, z2, linewidth=2, color=c)

        if title: ax.set_title(title)
        if xlabel: ax.set_xlabel(xlabel)
        if ylabel: ax.set_ylabel(ylabel)
        if zlabel: ax.set_zlabel(zlabel)

        ax.set(xticklabels=[], yticklabels=[], zticklabels=[])

        if IS_PYODIDE:
            buf = io.BytesIO()
            plt.savefig(buf, format='png', bbox_inches='tight')
            plt.close(fig)
            buf.seek(0)
            img_b64 = base64.b64encode(buf.read()).decode('utf-8')
            return f"data:image/png;base64,{img_b64}"
        else:
            return fig
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Input data with 6 columns (X1, Y1, Z1, X2, Y2, Z2).
Chart title.
Label for X-axis.
Label for Y-axis.
Label for Z-axis.
Area color.
Alpha transparency.