Heatmaps with Prompt AI

Becca Weng - October 9th, 2023

Heat maps are a powerful way of visualizing a variables across two dimensions. These dimensions are usually categorical or geographical, for example, many of us have encountered heatmaps on maps. You might have also encountered heatmaps as a way to visualize correlation matrices.

In this post, we'll use Einblick Prompt, the AI agent that can write and fix code and create beautiful charts to produce and customize a heatmap. Here's a snapshot of the Adidas sales dataset we'll be using.

All the following example prompts are available in a shared canvas, that has other example Prompts for other data visualizations and use-cases. You can simply open and fork the canvas below.

The written content, with accompanying YouTube videos, are available as part of our Prompt Example Library, on our website.

Prompt 1: Basic heatmap

Plot the mean operating margin for every retailer and region, as a heatmap
# PROMPT: Plot the mean operating margin for every retailer and region, as a heatmap

# Import the necessary libraries
import seaborn as sns

# Create a pivot table using the grouped data, with 'Retailer' as the index, 'Region' as the columns, and the mean operating margin as the values
pivot_table = adidas_sales.pivot_table(index='Retailer', columns='Region', values='Operating Margin')

# Use a heatmap visualization to plot the pivot table
sns.heatmap(pivot_table)

Code Output:

Prompt example, red heatmap of operating margin for retailer and regionPrompt example, red heatmap of operating margin for retailer and region

Prompt 2: Add annotations

Since the generated heatmap did not include values on the actual heatmap, let's add them.

Add annotations

NOTE: Prompt created annotations, as asked, however, there is a simpler argument for the specific seaborn heatmap function being used. To learn more, check out our post on how to create seaborn heatmaps.

# CHANGE: Add annotations

# Import the necessary libraries
import seaborn as sns

# Create a pivot table using the grouped data, with 'Retailer' as the index, 'Region' as the columns, and the mean operating margin as the values
pivot_table = adidas_sales.pivot_table(index='Retailer', columns='Region', values='Operating Margin')

# Use a heatmap visualization to plot the pivot table
ax = sns.heatmap(pivot_table)

# Add white annotations to the heatmap
for i in range(len(pivot_table)):
    for j in range(len(pivot_table.columns)):
        ax.text(j+0.5, i+0.5, round(pivot_table.iloc[i, j], 2), ha='center', va='center', color='white')

# Display the plot
plt.show()

Code Output:

Prompt example, red heatmap of operating margin for retailer and region, with annotationsPrompt example, red heatmap of operating margin for retailer and region, with annotations

Prompt 3: Change color palette

If you want to change the color palette of the heatmap, you can just select Prompt > Change the above cell, and try the following prompt.

Turn heatmap blue
# CHANGE: Turn heatmap blue

# Import the necessary libraries
import seaborn as sns

# Create a pivot table using the grouped data, with 'Retailer' as the index, 'Region' as the columns, and the mean operating margin as the values
pivot_table = adidas_sales.pivot_table(index='Retailer', columns='Region', values='Operating Margin')

# Use a heatmap visualization to plot the pivot table
ax = sns.heatmap(pivot_table, cmap='Blues')  # Set the colormap to 'Blues'

# Add white annotations to the heatmap
for i in range(len(pivot_table)):
    for j in range(len(pivot_table.columns)):
        ax.text(j+0.5, i+0.5, round(pivot_table.iloc[i, j], 2), ha='center', va='center', color='white')

# Display the plot
plt.show()

Code Output:

Prompt example, blue heatmap of operating margin for retailer and region, with annotationsPrompt example, blue heatmap of operating margin for retailer and region, with annotations

Prompt 4: Change annotation color

If you want to check the values in those white squares, you might want to change the color of the annotations:

Use black annotations
# CHANGE: Use black annotations

# Import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Create a pivot table using the grouped data, with 'Retailer' as the index, 'Region' as the columns, and the mean operating margin as the values
pivot_table = adidas_sales.pivot_table(index='Retailer', columns='Region', values='Operating Margin')

# Use a heatmap visualization to plot the pivot table
ax = sns.heatmap(pivot_table, cmap='Blues')  # Set the colormap to 'Blues'

# Add black annotations to the heatmap
for i in range(len(pivot_table)):
    for j in range(len(pivot_table.columns)):
        ax.text(j+0.5, i+0.5, round(pivot_table.iloc[i, j], 2), ha='center', va='center', color='black')  # Change color to black

# Display the plot
plt.show()

Code Output:

Prompt example, blue heatmap of operating margin for retailer and region, with black annotationsPrompt example, blue heatmap of operating margin for retailer and region, with black annotations

Prompt 5: Add gridlines

You can also add gridlines if you want to define the heatmap a bit more.

Add black gridlines

Once the data was subsetted, I asked Prompt to create the chart.

# CHANGE: Add black gridlines

# Import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Create a pivot table using the grouped data, with 'Retailer' as the index, 'Region' as the columns, and the mean operating margin as the values
pivot_table = adidas_sales.pivot_table(index='Retailer', columns='Region', values='Operating Margin')

# Use a heatmap visualization to plot the pivot table
ax = sns.heatmap(pivot_table, cmap='Blues', linewidths=0.5, linecolor='black')  # Set the colormap to 'Blues' and add black gridlines

# Add black annotations to the heatmap
for i in range(len(pivot_table)):
    for j in range(len(pivot_table.columns)):
        ax.text(j+0.5, i+0.5, round(pivot_table.iloc[i, j], 2), ha='center', va='center', color='black')  # Change color to black

# Display the plot
plt.show()

Code Output:

Prompt example, blue heatmap of operating margin for retailer and region, with gridlinesPrompt example, blue heatmap of operating margin for retailer and region, with gridlines

BONUS: Make a pivot table

If you want to table of values instead of the visualization, you can also ask Prompt to create a pivot table for you. We have provided examples of how Prompt can clean data and transform string data as well in our Prompt Example Library.

Create a pivot table of the mean operating margin by region and retailer

Once the data was subsetted, I asked Prompt to create the chart.

# PROMPT: Create a pivot table of the mean operating margin by region and retailer

# Import the necessary libraries
import pandas as pd

# Create the pivot table
pivot_table = adidas_sales.pivot_table(values='Operating Margin', index=['Region', 'Retailer'], aggfunc='mean')

# Display the pivot table
pivot_table

Code Output:

Example of pivot table created by Einblick Prompt AIExample of pivot table created by Einblick Prompt AI

About

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators