STEP

Excel Usage

=STEP(data, title, xlabel, ylabel, step_color, where, grid, legend)
  • data (list[list], required): Input 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.
  • step_color (str, optional, default: null): Step color.
  • where (str, optional, default: “pre”): Step location (‘pre’, ‘post’, ‘mid’).
  • grid (str, optional, default: “true”): Show grid lines.
  • legend (str, optional, default: “false”): Show legend.

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

Examples

Example 1: Basic step plot

Inputs:

data
1 1
2 3
3 2
4 4

Excel formula:

=STEP({1,1;2,3;3,2;4,4})

Expected output:

"chart"

Example 2: Step plot with title and axis labels

Inputs:

data title xlabel ylabel
1 1 Step Function X Y
2 3
3 2

Excel formula:

=STEP({1,1;2,3;3,2}, "Step Function", "X", "Y")

Expected output:

"chart"

Example 3: Multiple step series with legend

Inputs:

data legend
1 1 2 true
2 3 4
3 2 3

Excel formula:

=STEP({1,1,2;2,3,4;3,2,3}, "true")

Expected output:

"chart"

Example 4: Custom step location and color

Inputs:

data where step_color
1 1 mid red
2 3
3 2

Excel formula:

=STEP({1,1;2,3;3,2}, "mid", "red")

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 step(data, title=None, xlabel=None, ylabel=None, step_color=None, where='pre', grid='true', legend='false'):
    """
    Create a step plot from data.

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.step.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): Input 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.
        step_color (str, optional): Step color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is None.
        where (str, optional): Step location ('pre', 'post', 'mid'). Valid options: Pre, Mid, Post. Default is 'pre'.
        grid (str, optional): Show grid lines. Valid options: True, False. Default is 'true'.
        legend (str, optional): Show legend. Valid options: True, False. Default is 'false'.

    Returns:
        object: Matplotlib Figure object (standard Python) or base64 encoded PNG string (Pyodide).
    """
    try:
        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.ndim != 2 or arr.shape[1] < 2:
            return "Error: Data must have at least 2 columns (X, Y)."

        # Extract X and Y series
        x = arr[:, 0]
        y_series = [arr[:, i] for i in range(1, arr.shape[1])]

        # Create figure
        fig, ax = plt.subplots(figsize=(6, 4))

        # Plot step
        if len(y_series) == 1:
            ax.step(x, y_series[0], where=where, color=step_color if step_color else None)
        else:
            for i, y in enumerate(y_series):
                ax.step(x, y, where=where, label=f"Series {i+1}")

        # Set labels and title
        if title:
            ax.set_title(title)
        if xlabel:
            ax.set_xlabel(xlabel)
        if ylabel:
            ax.set_ylabel(ylabel)

        # Grid and legend
        if grid == "true":
            ax.grid(True, alpha=0.3)
        if legend == "true" and len(y_series) > 1:
            ax.legend()

        plt.tight_layout()

        if IS_PYODIDE:
            buf = io.BytesIO()
            plt.savefig(buf, format='png')
            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