TRICONTOUR_FILLED

Excel Usage

=TRICONTOUR_FILLED(data, title, xlabel, ylabel, color_map, levels)
  • data (list[list], required): Input data with 3 columns (X, Y, Z).
  • 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.
  • color_map (str, optional, default: “viridis”): Color map for regions.
  • levels (int, optional, default: 10): Number of contour levels.

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

Example 1: Simple filled unstructured contour

Inputs:

data title
0 0 1 Filled Triangular Contour
1 0 2
0 1 3
1 1 4
0.5 0.5 5

Excel formula:

=TRICONTOUR_FILLED({0,0,1;1,0,2;0,1,3;1,1,4;0.5,0.5,5}, "Filled Triangular Contour")

Expected output:

"chart"

Example 2: Filled contour with plasma map

Inputs:

data color_map
0 0 1 plasma
2 0 10
1 2 5

Excel formula:

=TRICONTOUR_FILLED({0,0,1;2,0,10;1,2,5}, "plasma")

Expected output:

"chart"

Example 3: High resolution triangular filled

Inputs:

data levels
0 0 0 15
1 0 1
0.5 0.866 2
0.5 0.288 5

Excel formula:

=TRICONTOUR_FILLED({0,0,0;1,0,1;0.5,0.866,2;0.5,0.288,5}, 15)

Expected output:

"chart"

Example 4: Filled contour with magma map

Inputs:

data color_map
0 0 1 magma
1 0 2
0 1 3
1 1 4

Excel formula:

=TRICONTOUR_FILLED({0,0,1;1,0,2;0,1,3;1,1,4}, "magma")

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 tricontour_filled(data, title=None, xlabel=None, ylabel=None, color_map='viridis', levels=10):
    """
    Draw filled contour regions on an unstructured triangular grid.

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tricontourf.html

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

    Args:
        data (list[list]): Input data with 3 columns (X, Y, Z).
        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.
        color_map (str, optional): Color map for regions. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
        levels (int, optional): Number of contour levels. Default is 10.

    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."

        # Convert to numpy array
        try:
            arr = np.array(data, dtype=float)
        except Exception:
            return "Error: Data must be numeric."

        if arr.shape[1] < 3:
            return "Error: Data must have at least 3 columns (X, Y, Z)."

        # Extract coordinates
        x, y, z = arr[:, 0], arr[:, 1], arr[:, 2]

        # Create figure
        fig, ax = plt.subplots(figsize=(8, 6))

        # Plot filled tricontour
        ax.tricontourf(x, y, z, levels=levels, cmap=color_map)

        # Set labels and title
        if title:
            ax.set_title(title)
        if xlabel:
            ax.set_xlabel(xlabel)
        if ylabel:
            ax.set_ylabel(ylabel)

        plt.tight_layout()

        if IS_PYODIDE:
            buf = io.BytesIO()
            plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
            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)}"

Online Calculator

Input data with 3 columns (X, Y, Z).
Chart title.
Label for X-axis.
Label for Y-axis.
Color map for regions.
Number of contour levels.