QUIVER

Excel Usage

=QUIVER(data, title, xlabel, ylabel, plot_color, scale)
  • 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.
  • plot_color (str, optional, default: null): Arrow color.
  • scale (float, optional, default: 1): Scaler for arrow length.

Returns (object): Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).

Examples

Example 1: Basic quiver plot

Inputs:

data
0 0 1 0
1 0 0 1
0 1 -1 0
1 1 0 -1

Excel formula:

=QUIVER({0,0,1,0;1,0,0,1;0,1,-1,0;1,1,0,-1})

Expected output:

"chart"

Example 2: Quiver plot with red arrows

Inputs:

data plot_color
0 0 1 1 red
1 0 -1 1
0 1 1 -1
1 1 -1 -1

Excel formula:

=QUIVER({0,0,1,1;1,0,-1,1;0,1,1,-1;1,1,-1,-1}, "red")

Expected output:

"chart"

Example 3: Quiver plot with labels and title

Inputs:

data title xlabel ylabel
0 0 1 0 Vector Field X Y
1 0 0 1
2 0 -1 0

Excel formula:

=QUIVER({0,0,1,0;1,0,0,1;2,0,-1,0}, "Vector Field", "X", "Y")

Expected output:

"chart"

Example 4: Quiver plot with custom scale

Inputs:

data scale
0 0 2 2 5
1 1 -2 2
2 0 -2 -2

Excel formula:

=QUIVER({0,0,2,2;1,1,-2,2;2,0,-2,-2}, 5)

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 quiver(data, title=None, xlabel=None, ylabel=None, plot_color=None, scale=1):
    """
    Create a quiver plot (vector field arrows).

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.quiver.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.
        plot_color (str, optional): Arrow color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is None.
        scale (float, optional): Scaler for arrow length. 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"

        # Create quiver plot
        fig, ax = plt.subplots(figsize=(8, 6))

        # Apply color if specified
        quiver_kwargs = {}
        if plot_color:
            quiver_kwargs['color'] = plot_color
        if scale != 1.0:
            quiver_kwargs['scale'] = scale

        ax.quiver(X, Y, U, V, **quiver_kwargs)

        if title:
            ax.set_title(title)
        if xlabel:
            ax.set_xlabel(xlabel)
        if ylabel:
            ax.set_ylabel(ylabel)

        ax.axis('equal')

        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)}"

Online Calculator