Skip to main content
Glama

plot_multiple_functions

Plot multiple mathematical functions simultaneously on a single graph to compare relationships and visualize interactions between equations.

Instructions

Plots multiple functions on the same graph.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
formulasYes
x_rangeNo
y_rangeNo

Implementation Reference

  • src/main.py:198-198 (registration)
    Registers the 'plot_multiple_functions' tool using the @mcp.tool() decorator.
    @mcp.tool()
  • The main handler function that plots multiple mathematical functions on a single graph using SymPy's plotting capabilities. It processes each formula, appends series to a plot, saves to desktop, encodes as base64 data URI, and returns the image.
    async def plot_multiple_functions(
        ctx: MCPContext,
        formulas: List[str],
        x_range: Optional[List[float]] = [-10, 10],
        y_range: Optional[List[float]] = None,
    ) -> str:
        """Plots multiple functions on the same graph."""
        try:
            await ctx.progress.start(total=len(formulas) + 1, message="Starting multi-plot...")
            x = sympy.symbols('x')
            p = sympy.plot(show=False)
            
            for i, formula in enumerate(formulas):
                await ctx.progress.report(i + 1, message=f"Processing formula {i+1}: {formula}")
                expr = sympy.sympify(formula)
                line_label = formula
                line_color = f"C{i}"
                series = sympy.plot(expr, (x, x_range[0], x_range[1]), show=False, line_color=line_color, label=line_label)[0]
                p.append(series)
    
            if x_range:
                p.xlim = x_range
            if y_range:
                p.ylim = y_range
            
            p.legend = True
    
            # Save the plot to a file
            desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
            save_dir = os.path.join(desktop_path, 'Desmos-MCP')
            os.makedirs(save_dir, exist_ok=True)
            timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
            filename = f"multi_plot_{timestamp}.png"
            p.save(os.path.join(save_dir, filename))
    
            await ctx.progress.report(len(formulas) + 1, message="Encoding image...")
            buf = io.BytesIO()
            p.save(buf)
            buf.seek(0)
            img_base64 = base64.b64encode(buf.read()).decode('utf-8')
            data_uri = f"data:image/png;base64,{img_base64}"
            
            await ctx.progress.end()
            return f"Successfully plotted {len(formulas)} functions. Image: {data_uri}"
    
        except Exception as e:
            await ctx.progress.end()
            return f"Error plotting multiple functions. Details: {e}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TheGrSun/Desmos-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server