PCOLORMESH
Excel Usage
=PCOLORMESH(data, title, xlabel, ylabel, color_map, colorbar)
data(list[list], required): 2D array of intensity values (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.colorbar(str, optional, default: “true”): Show colorbar.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Example 1: Basic 5x5 pseudocolor mesh
Inputs:
| data | title | ||||
|---|---|---|---|---|---|
| 1 | 2 | 3 | 2 | 1 | Pseudocolor Mesh |
| 2 | 3 | 4 | 3 | 2 | |
| 3 | 4 | 5 | 4 | 3 | |
| 2 | 3 | 4 | 3 | 2 | |
| 1 | 2 | 3 | 2 | 1 |
Excel formula:
=PCOLORMESH({1,2,3,2,1;2,3,4,3,2;3,4,5,4,3;2,3,4,3,2;1,2,3,2,1}, "Pseudocolor Mesh")
Expected output:
"chart"
Example 2: Mesh with magma colormap
Inputs:
| data | color_map | ||
|---|---|---|---|
| 1 | 5 | 2 | magma |
| 4 | 2 | 6 |
Excel formula:
=PCOLORMESH({1,5,2;4,2,6}, "magma")
Expected output:
"chart"
Example 3: Mesh without colorbar
Inputs:
| data | colorbar | |
|---|---|---|
| 1 | 1 | false |
| 1 | 1 |
Excel formula:
=PCOLORMESH({1,1;1,1}, "false")
Expected output:
"chart"
Example 4: Mesh with cividis colormap
Inputs:
| data | color_map | |
|---|---|---|
| 1 | 2 | cividis |
| 3 | 4 |
Excel formula:
=PCOLORMESH({1,2;3,4}, "cividis")
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 pcolormesh(data, title=None, xlabel=None, ylabel=None, color_map='viridis', colorbar='true'):
"""
Create a pseudocolor plot with a rectangular grid.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolormesh.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): 2D array of intensity values (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. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
colorbar (str, optional): Show colorbar. Valid options: True, False. Default is 'true'.
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:
Z = np.array(data, dtype=float)
except Exception:
return "Error: Data must be numeric."
if Z.ndim != 2:
return "Error: Data must be a 2D array (matrix)."
# Create X and Y coordinates (regular grid inferred from Z)
rows, cols = Z.shape
X, Y = np.meshgrid(np.arange(cols), np.arange(rows))
# Create figure
fig, ax = plt.subplots(figsize=(8, 6))
# Plot pcolormesh
im = ax.pcolormesh(X, Y, Z, cmap=color_map, shading='auto')
# Add colorbar if requested
if colorbar == "true":
plt.colorbar(im, ax=ax)
# 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
2D array of intensity values (Z).
Chart title.
Label for X-axis.
Label for Y-axis.
Color map.
Show colorbar.