CONTOUR_FILLED
Excel Usage
=CONTOUR_FILLED(data, title, xlabel, ylabel, color_map, levels, 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.color_map(str, optional, default: “viridis”): Color map for contours.levels(int, optional, default: 10): Number of contour levels.colorbar(str, optional, default: “true”): Show colorbar.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Examples
Example 1: Basic filled contour plot
Inputs:
| data | |||
|---|---|---|---|
| 1 | 2 | 3 | 4 |
| 2 | 4 | 6 | 8 |
| 3 | 6 | 9 | 12 |
| 4 | 8 | 12 | 16 |
Excel formula:
=CONTOUR_FILLED({1,2,3,4;2,4,6,8;3,6,9,12;4,8,12,16})
Expected output:
"chart"
Example 2: Filled contour with inferno colormap
Inputs:
| data | color_map | ||
|---|---|---|---|
| 0 | 1 | 2 | inferno |
| 1 | 2 | 3 | |
| 2 | 3 | 4 |
Excel formula:
=CONTOUR_FILLED({0,1,2;1,2,3;2,3,4}, "inferno")
Expected output:
"chart"
Example 3: Filled contour with labels and title
Inputs:
| data | title | xlabel | ylabel | ||
|---|---|---|---|---|---|
| 1 | 2 | 3 | Filled Contour | X-axis | Y-axis |
| 2 | 4 | 6 | |||
| 3 | 6 | 9 |
Excel formula:
=CONTOUR_FILLED({1,2,3;2,4,6;3,6,9}, "Filled Contour", "X-axis", "Y-axis")
Expected output:
"chart"
Example 4: Filled contour without colorbar
Inputs:
| data | colorbar | ||||
|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | false |
| 2 | 3 | 4 | 5 | 6 | |
| 3 | 4 | 5 | 6 | 7 |
Excel formula:
=CONTOUR_FILLED({1,2,3,4,5;2,3,4,5,6;3,4,5,6,7}, "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
import io
import base64
import numpy as np
def contour_filled(data, title=None, xlabel=None, ylabel=None, color_map='viridis', levels=10, colorbar='true'):
"""
Create a filled contour plot.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.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.
color_map (str, optional): Color map for contours. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
levels (int, optional): Number of contour levels. Default is 10.
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"
# Convert to numpy array
Z = []
for row in data:
z_row = []
for val in row:
try:
z_row.append(float(val))
except (TypeError, ValueError):
z_row.append(0)
Z.append(z_row)
Z = np.array(Z)
if Z.size == 0:
return "Error: No valid data found"
# Create X and Y coordinates
rows, cols = Z.shape
X = np.arange(cols)
Y = np.arange(rows)
X, Y = np.meshgrid(X, Y)
# Create filled contour plot
fig, ax = plt.subplots(figsize=(8, 6))
CS = ax.contourf(X, Y, Z, levels=levels, cmap=color_map)
if colorbar == "true":
plt.colorbar(CS, ax=ax)
if title:
ax.set_title(title)
if xlabel:
ax.set_xlabel(xlabel)
if ylabel:
ax.set_ylabel(ylabel)
if IS_PYODIDE:
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight')
plt.close(fig)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
return f"data:image/png;base64,{img_base64}"
else:
return fig
except Exception as e:
return f"Error: {str(e)}"