SCATTER
Excel Usage
=SCATTER(data, title, xlabel, ylabel, scatter_color, scatter_marker, point_size, grid, legend)
data(list[list], required): Input data. Supports multiple columns (X, Y1, Y2…).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.scatter_color(str, optional, default: null): Point color.scatter_marker(str, optional, default: “o”): Marker style.point_size(float, optional, default: 20): Size of points.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 XY scatter plot
Inputs:
| data | |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 6 |
| 4 | 8 |
Excel formula:
=SCATTER({1,2;2,4;3,6;4,8})
Expected output:
"chart"
Example 2: Scatter plot with title and axis labels
Inputs:
| data | title | xlabel | ylabel | |
|---|---|---|---|---|
| 1 | 2 | XY Data | X Values | Y Values |
| 2 | 4 | |||
| 3 | 6 |
Excel formula:
=SCATTER({1,2;2,4;3,6}, "XY Data", "X Values", "Y Values")
Expected output:
"chart"
Example 3: Multiple Y series with legend
Inputs:
| data | legend | ||
|---|---|---|---|
| 1 | 2 | 3 | true |
| 2 | 4 | 5 | |
| 3 | 6 | 7 |
Excel formula:
=SCATTER({1,2,3;2,4,5;3,6,7}, "true")
Expected output:
"chart"
Example 4: Custom marker, color, and size
Inputs:
| data | scatter_marker | scatter_color | point_size | |
|---|---|---|---|---|
| 1 | 2 | s | red | 50 |
| 2 | 4 | |||
| 3 | 6 |
Excel formula:
=SCATTER({1,2;2,4;3,6}, "s", "red", 50)
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 scatter(data, title=None, xlabel=None, ylabel=None, scatter_color=None, scatter_marker='o', point_size=20, grid='true', legend='false'):
"""
Create an XY scatter plot from data.
See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input data. Supports multiple columns (X, Y1, Y2...).
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.
scatter_color (str, optional): Point color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is None.
scatter_marker (str, optional): Marker style. Valid options: Point, Pixel, Circle, Square, Triangle Down, Triangle Up. Default is 'o'.
point_size (float, optional): Size of points. Default is 20.
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 scatter points
if len(y_series) == 1:
ax.scatter(x, y_series[0], s=point_size, color=scatter_color if scatter_color else None, marker=scatter_marker)
else:
for i, y in enumerate(y_series):
ax.scatter(x, y, s=point_size, marker=scatter_marker, 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)}"