SURFACE_3D
Excel Usage
=SURFACE_3D(data, title, xlabel, ylabel, zlabel, color_map, colorbar)
data(list[list], required): Input Z-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.zlabel(str, optional, default: null): Label for Z-axis.color_map(str, optional, default: “viridis”): Color map for the surface.colorbar(str, optional, default: “true”): Show colorbar.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Examples
Example 1: Basic 3D surface plot
Inputs:
| data | ||
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
Excel formula:
=SURFACE_3D({1,2,3;4,5,6;7,8,9})
Expected output:
"chart"
Example 2: Surface with labels and colorbar
Inputs:
| data | title | xlabel | ylabel | zlabel | colorbar | ||
|---|---|---|---|---|---|---|---|
| 1 | 4 | 9 | Surface Plot | X | Y | Z | true |
| 2 | 5 | 10 | |||||
| 3 | 6 | 11 |
Excel formula:
=SURFACE_3D({1,4,9;2,5,10;3,6,11}, "Surface Plot", "X", "Y", "Z", "true")
Expected output:
"chart"
Example 3: Using plasma colormap
Inputs:
| data | color_map | ||
|---|---|---|---|
| 0 | 1 | 2 | plasma |
| 1 | 2 | 3 | |
| 2 | 3 | 4 | |
| 3 | 4 | 5 |
Excel formula:
=SURFACE_3D({0,1,2;1,2,3;2,3,4;3,4,5}, "plasma")
Expected output:
"chart"
Example 4: Surface without colorbar
Inputs:
| data | colorbar | ||
|---|---|---|---|
| 5 | 10 | 15 | false |
| 20 | 25 | 30 | |
| 35 | 40 | 45 |
Excel formula:
=SURFACE_3D({5,10,15;20,25,30;35,40,45}, "false")
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
import io
import base64
import numpy as np
def surface_3d(data, title=None, xlabel=None, ylabel=None, zlabel=None, color_map='viridis', colorbar='true'):
"""
Create a 3D surface plot.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.plot_surface.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input Z-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.
zlabel (str, optional): Label for Z-axis. Default is None.
color_map (str, optional): Color map for the surface. 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 all(isinstance(row, list) for row in data):
return "Error: Invalid input - data must be a 2D list"
# Validate and convert to numpy array
try:
z_data = np.array(data, dtype=float)
except (TypeError, ValueError) as e:
return f"Error: Non-numeric values in data: {str(e)}"
if z_data.ndim != 2:
return "Error: Data must be a 2D array"
if z_data.shape[0] < 2 or z_data.shape[1] < 2:
return "Error: Surface requires at least 2x2 grid"
# Create X and Y meshgrid
rows, cols = z_data.shape
x_grid = np.arange(cols)
y_grid = np.arange(rows)
X, Y = np.meshgrid(x_grid, y_grid)
# Create figure
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Create surface plot
surf = ax.plot_surface(X, Y, z_data, cmap=color_map, alpha=0.8, edgecolor='none')
# Set labels
if title:
ax.set_title(title)
if xlabel:
ax.set_xlabel(xlabel)
if ylabel:
ax.set_ylabel(ylabel)
if zlabel:
ax.set_zlabel(zlabel)
# Add colorbar if requested
if colorbar == "true":
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)
# Return based on platform
if IS_PYODIDE:
buf = io.BytesIO()
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close(fig)
return f"data:image/png;base64,{img_base64}"
else:
return fig
except Exception as e:
return f"Error: {str(e)}"