POLAR_LINE

Excel Usage

=POLAR_LINE(data, title, plot_color, linestyle, linewidth, legend)
  • data (list[list], required): Input data (Theta, R).
  • title (str, optional, default: null): Chart title.
  • plot_color (str, optional, default: null): Line color.
  • linestyle (str, optional, default: “-”): Line style (e.g., ‘-’, ‘–’).
  • linewidth (float, optional, default: 1.5): Line width.
  • legend (str, optional, default: “false”): Show legend.

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

Examples

Example 1: Basic polar line forming a circle

Inputs:

data
0 2
1.571 2
3.142 2
4.712 2
6.283 2

Excel formula:

=POLAR_LINE({0,2;1.571,2;3.142,2;4.712,2;6.283,2})

Expected output:

"chart"

Example 2: Polar line with blue color and dashed style

Inputs:

data plot_color linestyle
0 1 blue
0.785 1.5
1.571 2
2.356 2.5
3.142 3

Excel formula:

=POLAR_LINE({0,1;0.785,1.5;1.571,2;2.356,2.5;3.142,3}, "blue", "--")

Expected output:

"chart"

Example 3: Polar line spiral pattern

Inputs:

data
0 0.5
0.785 1
1.571 1.5
2.356 2
3.142 2.5
3.927 3

Excel formula:

=POLAR_LINE({0,0.5;0.785,1;1.571,1.5;2.356,2;3.142,2.5;3.927,3})

Expected output:

"chart"

Example 4: Polar line with title and legend

Inputs:

data title legend
0 1 Polar Line true
1.571 2
3.142 1.5
4.712 2.5

Excel formula:

=POLAR_LINE({0,1;1.571,2;3.142,1.5;4.712,2.5}, "Polar Line", "true")

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 polar_line(data, title=None, plot_color=None, linestyle='-', linewidth=1.5, legend='false'):
    """
    Create a line plot in polar coordinates.

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.plot.html

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

    Args:
        data (list[list]): Input data (Theta, R).
        title (str, optional): Chart title. Default is None.
        plot_color (str, optional): Line color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is None.
        linestyle (str, optional): Line style (e.g., '-', '--'). Valid options: Solid, Dashed, Dotted, Dash-dot. Default is '-'.
        linewidth (float, optional): Line width. Default is 1.5.
        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).
    """
    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 theta and r columns
        if len(data) < 1 or len(data[0]) < 2:
            return "Error: Data must have at least 2 columns (Theta, R)"

        theta = []
        r = []
        for row in data:
            if len(row) >= 2:
                try:
                    theta.append(float(row[0]))
                    r.append(float(row[1]))
                except (TypeError, ValueError):
                    continue

        if len(theta) == 0:
            return "Error: No valid numeric data found"

        # Create polar plot
        fig = plt.figure(figsize=(8, 6))
        ax = fig.add_subplot(111, projection='polar')

        # Apply color if specified
        plot_kwargs = {'linestyle': linestyle, 'linewidth': linewidth}
        if plot_color:
            plot_kwargs['color'] = plot_color

        ax.plot(theta, r, **plot_kwargs)

        if title:
            ax.set_title(title)

        if legend == "true":
            ax.legend(['Data'])

        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