Skip to main content
Glama

create_graph

Generate graphs and plots from data files using matplotlib/seaborn. Specify columns for axes, choose graph types like scatter or line, and save visualizations to files for analysis.

Instructions

Create a graph/plot from data using matplotlib/seaborn.

Args: file_path: Path to the data file x_column: Column name for x-axis (must be numeric) y_column: Column name for y-axis (must be numeric) output_path: Path where to save the graph image graph_type: Type of graph (scatter, line, bar, histogram) category_column: Optional categorical column for grouping/coloring

Returns: Information about the created graph

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
x_columnYes
y_columnYes
output_pathYes
graph_typeNoscatter
category_columnNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The complete create_graph tool handler function that creates scatter, line, bar, or histogram plots from data using matplotlib/seaborn. It validates inputs, loads data, handles categorical grouping, and saves the plot to a file.
    @mcp.tool()
    def create_graph(file_path: str, x_column: str, y_column: str, 
                    output_path: str, graph_type: str = "scatter", 
                    category_column: Optional[str] = None) -> str:
        """
        Create a graph/plot from data using matplotlib/seaborn.
        
        Args:
            file_path: Path to the data file
            x_column: Column name for x-axis (must be numeric)
            y_column: Column name for y-axis (must be numeric) 
            output_path: Path where to save the graph image
            graph_type: Type of graph (scatter, line, bar, histogram)
            category_column: Optional categorical column for grouping/coloring
        
        Returns:
            Information about the created graph
        """
        try:
            if not VISUALIZATION_AVAILABLE:
                return f"Error: {VISUALIZATION_ERROR}"
                
            import pandas as pd
            from pathlib import Path
            
            # Load the data
            file_extension = Path(file_path).suffix.lower()
            if file_extension == '.csv':
                df = pd.read_csv(file_path)
            elif file_extension == '.json':
                df = pd.read_json(file_path)
            elif file_extension in ['.xlsx', '.xls']:
                df = pd.read_excel(file_path)
            elif file_extension == '.tsv':
                df = pd.read_csv(file_path, sep='\t')
            else:
                df = pd.read_csv(file_path)
            
            # Validate columns exist
            if x_column not in df.columns:
                return f"Error: Column '{x_column}' not found in data"
            if y_column not in df.columns:
                return f"Error: Column '{y_column}' not found in data"
            if category_column and category_column not in df.columns:
                return f"Error: Category column '{category_column}' not found in data"
            
            # Ensure numeric columns are properly typed
            try:
                df[x_column] = pd.to_numeric(df[x_column], errors='coerce')
                df[y_column] = pd.to_numeric(df[y_column], errors='coerce')
            except:
                return f"Error: Could not convert {x_column} or {y_column} to numeric values"
            
            # Remove rows with NaN values in plotting columns
            plot_columns = [x_column, y_column]
            if category_column:
                plot_columns.append(category_column)
            df_clean = df[plot_columns].dropna()
            
            if len(df_clean) == 0:
                return "Error: No valid data points for plotting after removing NaN values"
            
            # Create the plot
            plt.figure(figsize=(10, 6))
            
            if graph_type == "scatter":
                if category_column:
                    sns.scatterplot(data=df_clean, x=x_column, y=y_column, hue=category_column, alpha=0.7)
                else:
                    plt.scatter(df_clean[x_column], df_clean[y_column], alpha=0.7)
            elif graph_type == "line":
                if category_column:
                    sns.lineplot(data=df_clean, x=x_column, y=y_column, hue=category_column)
                else:
                    plt.plot(df_clean[x_column], df_clean[y_column])
            elif graph_type == "bar":
                if category_column:
                    # Group by category and take mean of y values for each x value
                    grouped = df_clean.groupby([x_column, category_column])[y_column].mean().reset_index()
                    sns.barplot(data=grouped, x=x_column, y=y_column, hue=category_column)
                else:
                    grouped = df_clean.groupby(x_column)[y_column].mean()
                    plt.bar(grouped.index, grouped.values)
            elif graph_type == "histogram":
                if category_column:
                    for category in df_clean[category_column].unique():
                        subset = df_clean[df_clean[category_column] == category]
                        plt.hist(subset[y_column], alpha=0.7, label=str(category), bins=20)
                    plt.legend()
                else:
                    plt.hist(df_clean[y_column], bins=20, alpha=0.7)
                plt.xlabel(y_column)
                plt.ylabel('Frequency')
            else:
                return f"Error: Unsupported graph type '{graph_type}'. Use: scatter, line, bar, histogram"
            
            # Set labels and title
            plt.xlabel(x_column.replace('_', ' ').title())
            plt.ylabel(y_column.replace('_', ' ').title())
            
            title = f"{graph_type.title()} Plot: {y_column} vs {x_column}"
            if category_column:
                title += f" (grouped by {category_column})"
            plt.title(title)
            
            # Add grid for better readability
            plt.grid(True, alpha=0.3)
            
            # Adjust layout to prevent label cutoff
            plt.tight_layout()
            
            # Save the plot
            plt.savefig(output_path, dpi=300, bbox_inches='tight')
            plt.close()
            
            result = {
                "graph_created": True,
                "graph_type": graph_type,
                "x_column": x_column,
                "y_column": y_column,
                "category_column": category_column,
                "data_points": len(df_clean),
                "output_file": output_path,
                "file_size": Path(output_path).stat().st_size if Path(output_path).exists() else 0
            }
            
            return json.dumps(result, indent=2)
            
        except Exception as e:
            return f"Error creating graph: {str(e)}\n{traceback.format_exc()}"
  • Visualization library imports and availability check. Matplotlib and seaborn are imported with the Agg non-interactive backend to support server-side graph generation.
    # Try to import visualization packages early to detect missing dependencies
    try:
        import matplotlib
        matplotlib.use('Agg')  # Use non-interactive backend
        import matplotlib.pyplot as plt
        import seaborn as sns
        VISUALIZATION_AVAILABLE = True
    except ImportError as e:
        VISUALIZATION_AVAILABLE = False
        VISUALIZATION_ERROR = f"Visualization libraries not available: {e}. Please install matplotlib and seaborn."
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. While it mentions the tool creates and saves a graph image, it doesn't disclose important behavioral aspects like file format requirements, error conditions, whether it overwrites existing files, or what 'Information about the created graph' actually means in the return value.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (description, Args, Returns) and efficiently communicates the core functionality. Every sentence serves a purpose, though the main description could be slightly more detailed about the tool's scope and limitations.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a 6-parameter tool with no annotations, the description provides good parameter documentation but lacks behavioral context. The existence of an output schema helps with return values, but the description should better explain what the tool actually does beyond parameter mapping, especially given the complex data visualization domain.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description compensates well by explaining all 6 parameters in the Args section, including data types (numeric columns), optional status, and purpose. It provides meaningful context beyond just parameter names, though it could elaborate more on format requirements for file paths.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool creates a graph/plot from data using matplotlib/seaborn, specifying both the action (create) and resource (graph/plot). It distinguishes itself from sibling tools like create_correlation_heatmap and create_distribution_plots by being a general-purpose graph creation tool rather than specialized visualizations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like create_correlation_heatmap or create_distribution_plots. It doesn't mention prerequisites (e.g., needing data loaded first) or when other tools might be more appropriate for specific visualization needs.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/moeloubani/visidata-mcp'

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