Skip to Content

BASIC_CHART

Overview

The BASIC_CHART function generates a simple matplotlib chart (line or bar) from provided numeric data and returns the chart as a PNG image encoded as a base64 string. This allows embedding charts directly in Excel or web dashboards. The function uses the matplotlib library (documentation).

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

Usage

To use the function in Excel:

=BASIC_CHART(data, [chart_type], [title], [xlabel], [ylabel])
  • data (2D list, required): Numeric data for the chart (rows/columns).
  • chart_type (string, optional, default=“line”): ‘line’ or ‘bar’.
  • title (string, optional): Chart title.
  • xlabel (string, optional): X-axis label.
  • ylabel (string, optional): Y-axis label.

The function returns a PNG image as a base64 string (starting with data:image/png;base64, ...) for embedding. If an error occurs, a string error message is returned.

Examples

Example 1: Simple Line Chart

This example creates a line chart for a growth curve.

XY
12
24
38
416

In Excel:

=BASIC_CHART({1,2;2,4;3,8;4,16}, "line", "Growth Curve", "X Axis", "Y Axis")

Expected output: a string starting with data:image/png;base64, (the chart image).

Example 2: Bar Chart

CategoryValue
A10
B20
C15

In Excel:

=BASIC_CHART({"A",10;"B",20;"C",15}, "bar", "Category Values", "Category", "Value")

Expected output: a string starting with data:image/png;base64, (the chart image).

Python Code

options = {"insert_only":True} import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np import io import base64 def basic_chart(data, chart_type='line', title=None, xlabel=None, ylabel=None): """ Generates a matplotlib chart from 2D numeric data and returns a PNG image as a base64 string. Args: data: 2D list of numbers (Excel range or similar) chart_type: 'line' or 'bar' (default: 'line') title: Chart title (optional) xlabel: X-axis label (optional) ylabel: Y-axis label (optional) Returns: PNG image as base64 string starting with 'data:image/png;base64,' or error message string This example function is provided as-is without any representation of accuracy. """ if not isinstance(data, list) or not data or not isinstance(data[0], list): return "Error: Input data must be a 2D list." 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 two columns (X and Y)." x = arr[:, 0] y = arr[:, 1] plt.figure(figsize=(6, 4)) if chart_type == 'bar': plt.bar(x, y) else: plt.plot(x, y, marker='o') if title: plt.title(title) if xlabel: plt.xlabel(xlabel) if ylabel: plt.ylabel(ylabel) plt.tight_layout() buf = io.BytesIO() plt.savefig(buf, format='png') plt.close() buf.seek(0) img_bytes = buf.read() img_b64 = base64.b64encode(img_bytes).decode('utf-8') return f"data:image/png;base64,{img_b64}"

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on