How to beginner to intermediate · 4 min read

How to use AI to create data visualizations

Quick answer
Use AI models like gpt-4o to generate data visualization code by providing natural language prompts describing the desired chart. The AI outputs Python code using libraries such as matplotlib or seaborn, which you can run to create visualizations automatically.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0 matplotlib seaborn pandas

Setup

Install required Python packages and set your OpenAI API key as an environment variable.

  • Install packages: pip install openai matplotlib seaborn pandas
  • Set environment variable: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install openai matplotlib seaborn pandas

Step by step

Use the OpenAI gpt-4o model to generate Python code for a data visualization based on a natural language prompt, then execute the code to display the chart.

python
import os
from openai import OpenAI
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define prompt describing the visualization
prompt = (
    "Generate Python code using matplotlib and seaborn to create a bar chart "
    "showing average monthly sales for a fictional company. Include sample data."
)

# Request code generation from GPT-4o
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

code = response.choices[0].message.content
print("Generated code:\n", code)

# Execute the generated code safely
# For demonstration, we use exec here; in production, validate code before running
exec(code)
output
Generated code:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Sales': [200, 220, 250, 270, 300, 320]
}
df = pd.DataFrame(data)

plt.figure(figsize=(8,5))
sns.barplot(x='Month', y='Sales', data=df)
plt.title('Average Monthly Sales')
plt.show()

Common variations

You can customize AI-generated visualizations by:

  • Using different models like claude-3-5-haiku-20241022 for more detailed explanations.
  • Requesting code for other libraries such as plotly or bokeh for interactive charts.
  • Using async calls or streaming completions for real-time code generation.
python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

prompt = (
    "Write Python code using plotly to create an interactive line chart "
    "showing daily website visits over a week with sample data."
)

message = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=512,
    system="You are a helpful assistant.",
    messages=[{"role": "user", "content": prompt}]
)

print("Generated code:\n", message.content[0].text)
output
Generated code:
import plotly.express as px
import pandas as pd

data = {
    'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    'Visits': [120, 150, 170, 160, 180, 200, 220]
}
df = pd.DataFrame(data)

fig = px.line(df, x='Day', y='Visits', title='Daily Website Visits')
fig.show()

Troubleshooting

If the AI-generated code fails to run, check for:

  • Missing imports: ensure all libraries like matplotlib, seaborn, or pandas are installed.
  • Syntax errors: sometimes AI outputs incomplete code; verify and correct manually.
  • API errors: confirm your OPENAI_API_KEY is set correctly and has quota.

Key Takeaways

  • Use natural language prompts with gpt-4o to generate ready-to-run data visualization code.
  • Leverage popular Python libraries like matplotlib and seaborn for static charts, or plotly for interactive visuals.
  • Always validate and test AI-generated code before running in production environments.
  • Customize AI prompts to specify chart types, data, and styling for tailored visualizations.
  • Use environment variables for API keys and keep dependencies up to date for smooth integration.
Verified 2026-04 · gpt-4o, claude-3-5-haiku-20241022
Verify ↗