
One of the most important data science tasks is data visualization, which aims to uncover patterns and insights underneath your data and communicate them to your audience.Unfortunately, creating insightful plots within data science workflows written in programming languages like Python can sometimes be time-consuming and complex.
This article unveils a helpful approach to building your code-based data visualizations more efficiently with the aid of AI assistants like ChatGPT and Claude. Through two use case scenarios, we will provide you with tips and hints to leverage AI tools for crafting attractive data visualizations.
Use Case 1: Visualizing Monthly Sales Trends in Retail
Suppose you are a retail analyst and want to visualize monthly sales trends in your company across different product categories to spot seasonal patterns. You have a dataset containing monthly sales numbers for several categories (such as Electronics, Clothing, and Home Goods) over the past two years. If you don’t have the data and want to try the examples shown, don’t worry: use this code to generate a manageable dataset randomly.
import pandas as pd
# Random seed optionally set for reproducibility
np.random.seed(42)
# Sample monthly sales dataset (24 months) with added randomness
data = {
'Month': pd.date_range(start='2022-01-01', periods=24, freq='M'),
'Electronics': [5000 + i * 200 + (i % 12) * 150 + np.random.randint(-500, 500) for i in range(24)],
'Clothing': [7000 + i * 180 + (i % 12) * 130 + np.random.randint(-400, 400) for i in range(24)],
'Home Goods': [4500 + i * 170 + (i % 12) * 100 + np.random.randint(-300, 300) for i in range(24)],
}
df = pd.DataFrame(data).melt(id_vars="Month", var_name="Category", value_name="Sales")
The user prompt would be something like:
“Given the attached dataset, create a Python-based line plot showing monthly sales trends over the past two years for each product category (Electronics, Clothing, Home Goods). Use different colors for each category to make it easy to compare, and add a title and legend.”
You can attach the data to this prompt either by manually pasting it (if it is a rather small size), or attaching it as a CSV file, JSON file, or similar.
With a prompt like the one shown above and the attached data, the AI assistant might generate a code snippet for you to comfortably copy and paste into your code editor:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(12, 6))
for category in df['Category'].unique():
subset = df[df['Category'] == category]
plt.plot(subset['Month'], subset['Sales'], label=category)
plt.title("Monthly Sales Trends by Category")
plt.xlabel("Month")
plt.ylabel("Sales ($)")
plt.legend(title="Product Category")
plt.grid(True)
plt.show()
When executed in your development environment, the visualization will show up:

Do you find this interesting? Just wait for what comes next: generating code for an interactive data visualization.
Use Case 2: Visualizing Patient Recovery Rates by Age Group
Now you wear the hat of a health researcher who has collected data on recovery rates across various age groups and wants to create an interactive bar chart to visualize and spot differences among groups. Assume you have the data and want the AI assistant to help you generate the necessary code for an interactive Plotly visualization. Plotly is a multi-language visualization toolbox ideal for building interacting visualizations for reporting and dashboards.
Take for instance this small dataset of average recovery rates already collected per age group:
Age Group,Recovery Rate 20-24,87 25-29,82 30-34,91 35-39,88 40-44,77 45-49,73 50-54,69 55-59,65 60-64,60 65-69,58 70-74,54 75+,50
This is what a user prompt could look like:
“I have a CSV file with detailed recovery rate data for different age groups. Could you help me create an interactive bar chart using Plotly? I want each bar to display the recovery rate on hover, with the highest rate in a distinct color. Please read the data from the CSV, and include labeled axes and a title in the chart.”
In this case, even though the user did not specify so, the AI assistant may assume the need for loading the dataset from an external CSV file into a Pandas DataFrame before visualizing the data. This is a possible code you may get as a result:
import pandas as pd
import plotly.graph_objects as go
# Load data from CSV (replace this part with manually created data of your own if you prefer)
df = pd.read_csv('recovery_rates.csv')
# Creating interactive bar chart
fig = go.Figure(
data=[go.Bar(
x=df['Age Group'],
y=df['Recovery Rate'],
marker_color=['blue' if rate < df['Recovery Rate'].max() else 'green' for rate in df['Recovery Rate']],
text=df['Recovery Rate'],
textposition='auto'
)]
)
# Layout customization
fig.update_layout(
title="Patient Recovery Rates by Age Group",
xaxis_title="Age Group",
yaxis_title="Recovery Rate (%)",
yaxis_range=[0, 100]
)
# Show interactive plot
fig.show()
When executed, you will get a nice interactive graph chart that looks like this.

By looking at the above-generated code, you can easily see where to customize aspects like bar colors, layout, and so on.
Wrap Up
In this article, we discovered through two use cases how to efficiently create interactive and appealing data visualizations based on Python code and aided by AI. These hacks will undoubtedly help turbocharge your data science project workflows, leaving the burden of customizing visualization parameters to your AI assistant.
