STREAMPLOT
Excel Usage
=STREAMPLOT(data, title, xlabel, ylabel, color_map, density)
data(list[list], required): Input data (X, Y, U, V).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 streamlines.density(float, optional, default: 1): Density of streamlines.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Examples
Example 1: Basic streamplot
Inputs:
| data | |||
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 |
Excel formula:
=STREAMPLOT({0,0,1,0;1,0,1,0;0,1,1,1;1,1,1,1})
Expected output:
"chart"
Example 2: Streamplot with plasma colormap
Inputs:
| data | color_map | |||
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | plasma |
| 1 | 0 | 0 | 1 | |
| 2 | 0 | -1 | 0 | |
| 0 | 1 | 1 | 1 | |
| 1 | 1 | 0 | 0 | |
| 2 | 1 | -1 | 1 |
Excel formula:
=STREAMPLOT({0,0,1,0;1,0,0,1;2,0,-1,0;0,1,1,1;1,1,0,0;2,1,-1,1}, "plasma")
Expected output:
"chart"
Example 3: Streamplot with labels and title
Inputs:
| data | title | xlabel | ylabel | |||
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | Flow Field | X | Y |
| 1 | 0 | -1 | 1 | |||
| 0 | 1 | 1 | -1 | |||
| 1 | 1 | -1 | -1 |
Excel formula:
=STREAMPLOT({0,0,1,1;1,0,-1,1;0,1,1,-1;1,1,-1,-1}, "Flow Field", "X", "Y")
Expected output:
"chart"
Example 4: Streamplot with higher density
Inputs:
| data | density | |||
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 2 |
| 1 | 0 | 0 | 1 | |
| 0 | 1 | -1 | 0 | |
| 1 | 1 | 0 | -1 |
Excel formula:
=STREAMPLOT({0,0,1,0;1,0,0,1;0,1,-1,0;1,1,0,-1}, 2)
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 streamplot(data, title=None, xlabel=None, ylabel=None, color_map='viridis', density=1):
"""
Create a streamplot (vector field streamlines).
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.streamplot.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input data (X, Y, U, V).
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 streamlines. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
density (float, optional): Density of streamlines. Default is 1.
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"
# Extract X, Y, U, V columns
if len(data) < 1 or len(data[0]) < 4:
return "Error: Data must have at least 4 columns (X, Y, U, V)"
X = []
Y = []
U = []
V = []
for row in data:
if len(row) >= 4:
try:
X.append(float(row[0]))
Y.append(float(row[1]))
U.append(float(row[2]))
V.append(float(row[3]))
except (TypeError, ValueError):
continue
if len(X) == 0:
return "Error: No valid numeric data found"
# Convert to unique grid points
X_unique = sorted(list(set(X)))
Y_unique = sorted(list(set(Y)))
if len(X_unique) < 2 or len(Y_unique) < 2:
return "Error: Need at least 2 unique X and Y values for streamplot"
# Create meshgrid
X_grid, Y_grid = np.meshgrid(X_unique, Y_unique)
# Interpolate U and V onto grid
U_grid = np.zeros_like(X_grid)
V_grid = np.zeros_like(Y_grid)
for i in range(len(X)):
xi = X_unique.index(X[i])
yi = Y_unique.index(Y[i])
U_grid[yi, xi] = U[i]
V_grid[yi, xi] = V[i]
# Create streamplot
fig, ax = plt.subplots(figsize=(8, 6))
strm = ax.streamplot(X_grid, Y_grid, U_grid, V_grid, color=np.sqrt(U_grid**2 + V_grid**2),
cmap=color_map, density=density)
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)}"