TRIPCOLOR
Excel Usage
=TRIPCOLOR(data, title, xlabel, ylabel, color_map)
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 plot.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Example 1: Simple triangular pseudocolor
Inputs:
| data | title | ||
|---|---|---|---|
| 0 | 0 | 1 | Triangular Pseudocolor |
| 1 | 0 | 2 | |
| 0 | 1 | 3 | |
| 1 | 1 | 4 | |
| 0.5 | 0.5 | 5 |
Excel formula:
=TRIPCOLOR({0,0,1;1,0,2;0,1,3;1,1,4;0.5,0.5,5}, "Triangular Pseudocolor")
Expected output:
"chart"
Example 2: Pseudocolor with magma map
Inputs:
| data | color_map | ||
|---|---|---|---|
| 0 | 0 | 1 | magma |
| 2 | 0 | 10 | |
| 1 | 2 | 5 |
Excel formula:
=TRIPCOLOR({0,0,1;2,0,10;1,2,5}, "magma")
Expected output:
"chart"
Example 3: Pseudocolor with cividis map
Inputs:
| data | color_map | ||
|---|---|---|---|
| 0 | 0 | 1 | cividis |
| 1 | 1 | 5 | |
| 2 | 0 | 2 |
Excel formula:
=TRIPCOLOR({0,0,1;1,1,5;2,0,2}, "cividis")
Expected output:
"chart"
Example 4: Pseudocolor with inferno map
Inputs:
| data | color_map | ||
|---|---|---|---|
| 0 | 0 | 5 | inferno |
| 1 | 0 | 1 | |
| 0 | 1 | 10 |
Excel formula:
=TRIPCOLOR({0,0,5;1,0,1;0,1,10}, "inferno")
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 tripcolor(data, title=None, xlabel=None, ylabel=None, color_map='viridis'):
"""
Create a pseudocolor plot of an unstructured triangular grid.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tripcolor.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 plot. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
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 tripcolor
ax.tripcolor(x, y, z, 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 plot.