avatarJohn Loewen, PhD

Summarize

Ready-to-Use GPT-4 Prompts for Python Data Visualization

4 case study examples showing the benefits of using of GPT-4

DALL-E image: impressonist painting, in vivid color, of a data visualization

Python is the language of choice for many data professionals worldwide, and for a good reason.

The libraries available for data visualization are unmatched by any other programming language (matplotlib, seaborn, plotly, altair, etc).

This guide provides some case-study examples of you can elevate your data visualization skills by coupling Python with OpenAI’s AI language model, ChatGPT.

We will explore four specific ready-to-use prompts with ChatGPT, providing a detailed walkthrough of the code generated by each prompt, alongside an actionable tip, and a mistake to avoid.

Let’s get stuck into it.

Prompt 1: Constructing a Line Chart with Matplotlib

Prompt to GPT-4

“ChatGPT, please generate Python code for a line chart using Matplotlib that visualizes the trend of stock prices over ten days.”

ChatGPT Response:

import matplotlib.pyplot as plt
import numpy as np

# Let's assume the data is for ten consecutive days
days = np.arange(1,11)
# Stock prices are a hypothetical dataset here
stock_prices = np.random.randint(100, 200, size=10)
plt.plot(days, stock_prices)
plt.xlabel('Days')
plt.ylabel('Stock Prices')
plt.title('Trend of Stock Prices Over Ten Days')
plt.grid(True)
plt.show()

Code Description:

This snippet begins by importing the required libraries. ‘Days’ represents an array of ten days, and ‘stock_prices’ is an array of ten random integers representing the stock prices. The plt.plot() function is used to generate a line chart with days on the X-axis and stock prices on the Y-axis. The grid is enabled using plt.grid(True) to better visualize the price changes.

Actionable Tip:

Remember to add a grid to your line charts. Gridlines can significantly enhance the readability and context of the data points and trends in your chart.

Mistake to Avoid:

Avoid cluttering your chart with too many data lines- it can make your chart hard to read and interpret.

Prompt 2: Generating a Scatter Plot with Seaborn

Prompt to GPT-4:

ChatGPT, show me how to create a scatter plot using Seaborn to illustrate the relationship between the variables ‘age’ and ‘income’.

ChatGPT Response:

import seaborn as sns
import pandas as pd
import numpy as np

# Let's create a hypothetical dataset
np.random.seed(0)
age = np.random.randint(20, 60, 100)
income = np.random.randint(30000, 80000, 100)
df = pd.DataFrame({"Age": age, "Income": income})
sns.scatterplot(data=df, x="Age", y="Income")
plt.title('Relationship Between Age and Income')
plt.show()

Code Description:

The snippet starts with importing the necessary libraries. ‘Age’ and ‘Income’ are arrays of 100 random integers each. These arrays are combined into a Pandas DataFrame. The sns.scatterplot() function then generates a scatter plot with ‘Age’ on the X-axis and ‘Income’ on the Y-axis.

Actionable Tip:

Use scatter plots when you want to observe relationships between two numerical variables. These charts can reveal correlations, clusters, and outliers effectively.

Mistake to Avoid:

Do not use scatter plots for categorical variables, as they are designed to depict patterns or correlations between numerical variables.

Prompt 3: Building an Interactive Pie Chart with Plotly

Prompt to ChatGPT:

ChatGPT, could you generate code for an interactive pie chart using Plotly that represents the market share of five tech companies?

ChatGPT Response:

import plotly.graph_objects as go

# Sample data
companies = ['Company A', 'Company B', 'Company C', 'Company D', 'Company E']
market_share = [30, 25, 20, 15, 10]
fig = go.Figure(data=[go.Pie(labels=companies, values=market_share, hole=.3)])
fig.update_layout(title_text='Market Share of Tech Companies')
fig.show()

Code Description:

After importing the required library, the code defines two lists: ‘companies’ and ‘market_share’. The go.Pie() function creates a pie chart that’s made interactive through the hole parameter that generates a donut-like appearance, allowing for better distinction between segments.

Actionable Tip:

Interactive pie charts are beneficial when you want to represent proportional datasets and allow users to engage with individual data sections.

Mistake to Avoid:

Do not overload your pie chart with too many segments. When you have numerous categories, consider using a bar chart or histogram to avoid creating a cluttered pie chart that’s difficult to read.

Prompt 4: Visualizing Data on a Map with Folium

Prompt to GPT-4:

ChatGPT, help me generate Python code using Folium to plot the location of a specific landmark on a map.

ChatGPT Response:

import folium
# Let's assume we are plotting the location of the Eiffel Tower
location = [48.8584, 2.2945]
m = folium.Map(location=location, zoom_start=14)
folium.Marker(location, popup="<strong>Eiffel Tower</strong>").add_to(m)

m.save('eiffel_map.html')

Code Description:

After importing Folium, a location coordinate for the Eiffel Tower is defined. The folium.Map() function creates a base map focused on the given location with a defined zoom level. A marker is then added to the map at the specified location, with a popup label indicating the landmark.

The m.save() method allows the map to be saved as an html file for easy viewing.

Actionable Tip:

When representing geospatial data, use map visualizations. They provide an intuitive understanding of the geographical distribution of data.

Mistake to Avoid:

Avoid using high zoom levels for large geospatial datasets. This can make your map visually cluttered and slow to load.

In Summary…

Python, with its intuitive libraries and simple syntax, is a very useful language for data visualization.

Combining it with ChatGPT’s prompt engineering allows for coding without deep syntax recall. This combination takes care of the heavy lifting in data transformation, allowing the developer (you) to focus on interpreting your data and on creating meaningful stories from this data.

With these detailed ChatGPT prompts, we hope you’re ready to use it to simplify, and expedite, your Python data visualization projects.

Subscribe to DDIntel Here.

Visit our website here: https://www.datadriveninvestor.com

Join our network here: https://datadriveninvestor.com/collaborate

Python
AI
ChatGPT
Prompt Engineering
Data Science
Recommended from ReadMedium