Skip to main content

An AI-powered data visualization assistant using Plotly

Project description

Plot Agent

Tests PyPI version

An AI-powered data visualization assistant that helps users create Plotly visualizations in Python.

Built on LangGraph with tool-calling to reliably execute generated Plotly code in a sandbox and keep the current fig in sync.

Installation

You can install the package using pip:

pip install plot-agent

Usage

See more examples in /examples/ (via nbviewer so that can see the charts easily).

Here's a simple minimal example of how to use Plot Agent:

import pandas as pd
from plot_agent.agent import PlotAgent

# ensure OPENAI_API_KEY is set (env or .env); optional debug via PLOT_AGENT_DEBUG=1

# Create a sample dataframe
df = pd.DataFrame({
    'x': [1, 2, 3, 4, 5],
    'y': [10, 20, 30, 40, 50]
})

# Initialize the agent
agent = PlotAgent()

# Set the dataframe
agent.set_df(df)

# Process a visualization request
response = agent.process_message("Create a line plot of x vs y")

# Print generated code
print(agent.generated_code)

# Get fig
fig = agent.get_figure()
fig.show()

agent.generated_code:

import pandas as pd
import plotly.graph_objects as go

# Creating a line plot of x vs y
# Create a figure object
fig = go.Figure()

# Add a line trace to the figure
fig.add_trace(
    go.Scatter(
        x=df['x'],  # The x values
        y=df['y'],  # The y values
        mode='lines+markers',  # Display both lines and markers
        name='Line Plot',  # Name of the trace
        line=dict(color='blue', width=2)  # Specify line color and width
    )
)

# Adding titles and labels
fig.update_layout(
    title='Line Plot of x vs y',  # Plot title
    xaxis_title='x',  # x-axis label
    yaxis_title='y',  # y-axis label
    template='plotly_white'  # A clean layout
)

How it works

flowchart TD
    A[User message] --> B{LangGraph ReAct Agent}
    subgraph Tools
      T1[execute_plotly_code<br/>- runs code in sandbox<br/>- returns success/fig/error]
      T2[does_fig_exist]
      T3[view_generated_code]
    end
    B -- tool call --> T1
    T1 -- result --> B
    B -- optional --> T2
    B -- optional --> T3
    B --> C[AI response]
    C --> D{Agent wrapper}
    D -- persist messages --> B
    D -- extract code blocks --> E[Sandbox execution]
    E --> F[fig]
    F --> G[get_figure]
  • The LangGraph agent plans and decides when to call tools.
  • The wrapper persists full graph messages between turns and executes any returned code blocks to keep fig updated.
  • A safe execution environment runs code with an allowlist and a main-thread-only timeout.

Features

  • AI-powered visualization generation
  • Support for various Plotly chart types
  • Automatic data preprocessing
  • Interactive visualization capabilities
  • LangGraph-based tool calling and control flow
  • Debug logging via PlotAgent(debug=True) or PLOT_AGENT_DEBUG=1

Requirements

  • Python 3.8 or higher
  • Dependencies are automatically installed with the package

Development

  • Run unit tests:
make test
  • Execute all example notebooks:
make run-examples
  • Execute with debug logs enabled:
make run-examples-debug
  • Quick CLI repro that prints evolving code each step:
make run-example-script

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plot_agent-0.4.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plot_agent-0.4.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file plot_agent-0.4.0.tar.gz.

File metadata

  • Download URL: plot_agent-0.4.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for plot_agent-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a89b02803e569950c0836c5dfc6b54224b39b5ab98fed2f67b5577f7113ffb46
MD5 dcdf9fcef060aab6d523bc2f49966665
BLAKE2b-256 bedec84e869c91ed99e66383061b1ec6cb0a4e7180f79467465f1c8424b3f008

See more details on using hashes here.

File details

Details for the file plot_agent-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: plot_agent-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for plot_agent-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5370bbdb041ffd99613a58aafd393b2760ba5958de56db6b884f2f3b2c980b7f
MD5 d80056e37f68be04296cd698939e1058
BLAKE2b-256 25e0a70e8f06cafb4f54f152ad653ef8bd0795d60d56a040c08d95a07f0d90cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page