BARBS
Excel Usage
=BARBS(data, title, xlabel, ylabel, barb_color)
data(list[list], required): Input data with 4 columns (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.barb_color(str, optional, default: “blue”): Color of the barbs.
Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
Example 1: Simple wind barb field
Inputs:
| data | title | |||
|---|---|---|---|---|
| 1 | 1 | 10 | 0 | Wind Barbs |
| 1 | 2 | 20 | 5 | |
| 2 | 1 | 5 | 10 | |
| 2 | 2 | 15 | 15 |
Excel formula:
=BARBS({1,1,10,0;1,2,20,5;2,1,5,10;2,2,15,15}, "Wind Barbs")
Expected output:
"chart"
Example 2: Green wind barbs
Inputs:
| data | barb_color | |||
|---|---|---|---|---|
| 1 | 1 | 10 | 10 | green |
| 2 | 2 | 20 | 20 |
Excel formula:
=BARBS({1,1,10,10;2,2,20,20}, "green")
Expected output:
"chart"
Example 3: Red wind barbs with labels
Inputs:
| data | barb_color | xlabel | ylabel | |||
|---|---|---|---|---|---|---|
| 0 | 0 | 5 | 5 | red | East | North |
| 1 | 1 | 15 | 15 |
Excel formula:
=BARBS({0,0,5,5;1,1,15,15}, "red", "East", "North")
Expected output:
"chart"
Example 4: Black wind barbs
Inputs:
| data | barb_color | |||
|---|---|---|---|---|
| 0 | 0 | 30 | 0 | black |
| 0 | 1 | 0 | 30 |
Excel formula:
=BARBS({0,0,30,0;0,1,0,30}, "black")
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 barbs(data, title=None, xlabel=None, ylabel=None, barb_color='blue'):
"""
Plot a 2D field of wind barbs.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.barbs.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input data with 4 columns (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.
barb_color (str, optional): Color of the barbs. Valid options: Blue, Green, Red, Black. Default is 'blue'.
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] < 4:
return "Error: Data must have at least 4 columns (X, Y, U, V)."
# Extract coordinates
X, Y, U, V = arr[:, 0], arr[:, 1], arr[:, 2], arr[:, 3]
# Create figure
fig, ax = plt.subplots(figsize=(8, 6))
# Plot barbs
ax.barbs(X, Y, U, V, barbcolor=barb_color, flagcolor=barb_color)
# 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 4 columns (X, Y, U, V).
Chart title.
Label for X-axis.
Label for Y-axis.
Color of the barbs.