DOT_PLOT

Excel Usage

=DOT_PLOT(data, title, xlabel, ylabel, plot_color, marker, legend)
  • data (list[list], required): Input data (Labels, Values).
  • 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.
  • plot_color (str, optional, default: “blue”): Dot color.
  • marker (str, optional, default: “o”): Marker style.
  • legend (str, optional, default: “false”): Show legend.

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

Examples

Example 1: Simple Cleveland dot plot

Inputs:

data
Category A 25
Category B 40
Category C 15
Category D 30

Excel formula:

=DOT_PLOT({"Category A",25;"Category B",40;"Category C",15;"Category D",30})

Expected output:

"chart"

Example 2: Dot plot with custom labels

Inputs:

data title xlabel ylabel
Item 1 10 Item Comparison Values Items
Item 2 20
Item 3 15

Excel formula:

=DOT_PLOT({"Item 1",10;"Item 2",20;"Item 3",15}, "Item Comparison", "Values", "Items")

Expected output:

"chart"

Example 3: Dot plot with square markers

Inputs:

data plot_color marker
A 5 red s
B 10
C 8

Excel formula:

=DOT_PLOT({"A",5;"B",10;"C",8}, "red", "s")

Expected output:

"chart"

Example 4: Dot plot with legend and triangle markers

Inputs:

data legend marker plot_color
Group 1 50 true ^ green
Group 2 75
Group 3 60

Excel formula:

=DOT_PLOT({"Group 1",50;"Group 2",75;"Group 3",60}, "true", "^", "green")

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 dot_plot(data, title=None, xlabel=None, ylabel=None, plot_color='blue', marker='o', legend='false'):
    """
    Create a Cleveland dot plot from data.

    See: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.scatter.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.
        xlabel (str, optional): Label for X-axis. Default is None.
        ylabel (str, optional): Label for Y-axis. Default is None.
        plot_color (str, optional): Dot color. Valid options: Blue, Green, Red, Cyan, Magenta, Yellow, Black, White. Default is 'blue'.
        marker (str, optional): Marker style. Valid options: None, Point, Pixel, Circle, Square, Triangle Down, Triangle Up. Default is 'o'.
        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 len(data) < 1:
            return "Error: Data must be a non-empty list"

        # Extract labels and values
        labels = []
        values = []
        for row in data:
            if not isinstance(row, list) or len(row) < 2:
                continue
            try:
                labels.append(str(row[0]))
                values.append(float(row[1]))
            except (ValueError, TypeError):
                continue

        if len(labels) == 0 or len(values) == 0:
            return "Error: No valid data rows found"

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

        # Create dot plot (Cleveland style)
        y_pos = np.arange(len(labels))

        # Draw lines from 0 to value
        for i, val in enumerate(values):
            ax.plot([0, val], [i, i], 'k-', linewidth=1, alpha=0.5)

        # Draw dots
        ax.scatter(values, y_pos, color=plot_color, marker=marker, s=100, zorder=3)

        # Set labels
        ax.set_yticks(y_pos)
        ax.set_yticklabels(labels)

        if title:
            ax.set_title(title)
        if xlabel:
            ax.set_xlabel(xlabel)
        if ylabel:
            ax.set_ylabel(ylabel)

        # Handle legend
        if legend == "true":
            ax.legend(['Data'], loc="best")

        ax.grid(axis='x', alpha=0.3)
        plt.tight_layout()

        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