POLAR_BAR

Excel Usage

=POLAR_BAR(data, title, color_map, bottom, legend)
  • data (list[list], required): Input data (Theta, R).
  • title (str, optional, default: null): Chart title.
  • color_map (str, optional, default: “viridis”): Color map for bars.
  • bottom (float, optional, default: 0): Base of the bars.
  • 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 bar (rose diagram)

Inputs:

data
0 1
0.785 2
1.571 1.5
2.356 2.5
3.142 1.8
3.927 2.2
4.712 1.6
5.498 2.3

Excel formula:

=POLAR_BAR({0,1;0.785,2;1.571,1.5;2.356,2.5;3.142,1.8;3.927,2.2;4.712,1.6;5.498,2.3})

Expected output:

"chart"

Example 2: Polar bar with plasma colormap

Inputs:

data color_map
0 1.5 plasma
1.047 2.5
2.094 3
3.142 2
4.189 2.8
5.236 1.8

Excel formula:

=POLAR_BAR({0,1.5;1.047,2.5;2.094,3;3.142,2;4.189,2.8;5.236,1.8}, "plasma")

Expected output:

"chart"

Example 3: Polar bar with offset bottom

Inputs:

data bottom
0 2 1
1.571 3
3.142 2.5
4.712 3.5

Excel formula:

=POLAR_BAR({0,2;1.571,3;3.142,2.5;4.712,3.5}, 1)

Expected output:

"chart"

Example 4: Polar bar with title

Inputs:

data title
0 1 Rose Diagram
1.571 2
3.142 1.5
4.712 2.5

Excel formula:

=POLAR_BAR({0,1;1.571,2;3.142,1.5;4.712,2.5}, "Rose Diagram")

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_bar(data, title=None, color_map='viridis', bottom=0, legend='false'):
    """
    Create a bar chart in polar coordinates (also known as a Rose diagram).

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bar.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.
        color_map (str, optional): Color map for bars. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
        bottom (float, optional): Base of the bars. Default is 0.
        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"

        # Calculate bar width
        if len(theta) > 1:
            width = 2 * np.pi / len(theta)
        else:
            width = 0.5

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

        # Create colors from colormap
        cmap = plt.get_cmap(color_map)
        colors = cmap(np.linspace(0, 1, len(theta)))

        ax.bar(theta, r, width=width, bottom=bottom, color=colors)

        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