PIE

Excel Usage

=PIE(data, title, pie_colormap, explode, legend)
  • data (list[list], required): Input data (Labels, Values).
  • title (str, optional, default: null): Chart title.
  • pie_colormap (str, optional, default: “viridis”): Color map for slices.
  • explode (float, optional, default: 0): Explode value for slices.
  • legend (str, optional, default: “true”): Show legend.

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

Examples

Example 1: Basic pie chart with percentages

Inputs:

data
A 30
B 20
C 50

Excel formula:

=PIE({"A",30;"B",20;"C",50})

Expected output:

"chart"

Example 2: Pie chart with title and legend

Inputs:

data title legend
Q1 25 Quarterly Sales true
Q2 30
Q3 20
Q4 25

Excel formula:

=PIE({"Q1",25;"Q2",30;"Q3",20;"Q4",25}, "Quarterly Sales", "true")

Expected output:

"chart"

Example 3: Exploded pie chart

Inputs:

data explode
A 40 0.1
B 30
C 30

Excel formula:

=PIE({"A",40;"B",30;"C",30}, 0.1)

Expected output:

"chart"

Example 4: Custom color map

Inputs:

data pie_colormap
X 10 plasma
Y 20
Z 30

Excel formula:

=PIE({"X",10;"Y",20;"Z",30}, "plasma")

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
import matplotlib.cm as cm

def pie(data, title=None, pie_colormap='viridis', explode=0, legend='true'):
    """
    Create a pie chart from data.

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

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

    Args:
        data (list[list]): Input data (Labels, Values).
        title (str, optional): Chart title. Default is None.
        pie_colormap (str, optional): Color map for slices. Valid options: Viridis, Plasma, Inferno, Magma, Cividis. Default is 'viridis'.
        explode (float, optional): Explode value for slices. Default is 0.
        legend (str, optional): Show legend. Valid options: True, False. Default is 'true'.

    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."

        if len(data[0]) < 2:
            return "Error: Data must have at least 2 columns (Labels, Values)."

        # Extract labels and values
        labels = [str(row[0]) for row in data]
        try:
            values = [float(row[1]) for row in data]
        except Exception:
            return "Error: Values must be numeric."

        if any(v < 0 for v in values):
            return "Error: Pie chart values must be non-negative."

        if sum(values) == 0:
            return "Error: Sum of values must be greater than zero."

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

        # Get colors from colormap
        cmap = cm.get_cmap(pie_colormap)
        colors = [cmap(i / len(values)) for i in range(len(values))]

        # Create explode array if specified
        explode_array = None
        if explode > 0:
            explode_array = [explode] * len(values)

        # Create pie chart
        ax.pie(values, labels=labels, autopct='%1.1f%%', colors=colors, explode=explode_array)

        if title:
            ax.set_title(title)

        if legend == "true":
            ax.legend(labels, loc="best")

        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