📖 Scenario: You work as a data analyst for a retail company. You have collected sales data for different products and want to understand how the sales of these products relate to each other. Visualizing these relationships can help the team make better decisions.
🎯 Goal: Build a heatmap to visualize the correlation between sales of different products using Python.
📋 What You'll Learn
Create a pandas DataFrame with sales data for 4 products over 5 days.
Create a variable to store the correlation matrix of the DataFrame.
Use seaborn to create a heatmap of the correlation matrix.
Display the heatmap with clear labels.
💡 Why This Matters
🌍 Real World
Correlation heatmaps help businesses understand how different products' sales relate, guiding marketing and inventory decisions.
💼 Career
Data analysts and scientists use correlation heatmaps to quickly spot patterns and relationships in data, which is essential for data-driven decision making.
Progress0 / 4 steps
1
Create sales data DataFrame
Create a pandas DataFrame called sales_data with these exact columns and values: 'Product_A': [10, 15, 14, 20, 18], 'Product_B': [8, 12, 11, 19, 17], 'Product_C': [5, 7, 6, 9, 8], 'Product_D': [12, 18, 16, 22, 20].
Data Analysis Python
Hint
Use pd.DataFrame and pass a dictionary with product names as keys and lists of sales as values.
2
Calculate correlation matrix
Create a variable called correlation_matrix that stores the correlation matrix of sales_data using the corr() method.
Data Analysis Python
Hint
Use sales_data.corr() to get the correlation matrix.
3
Create heatmap of correlation matrix
Import seaborn as sns and matplotlib.pyplot as plt. Use sns.heatmap() to create a heatmap of correlation_matrix with annotations enabled (annot=True).
Data Analysis Python
Hint
Use sns.heatmap(correlation_matrix, annot=True) to create the heatmap.
4
Display the heatmap
Use plt.show() to display the heatmap plot.
Data Analysis Python
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What does a heatmap of correlation typically show in data analysis?
easy
A. The total sum of all numerical columns
B. The frequency of each value in a dataset
C. The order of rows in a DataFrame
D. The strength and direction of relationships between variables
Solution
Step 1: Understand correlation meaning
Correlation measures how two variables move together, either positively or negatively.
Step 2: Relate heatmap to correlation
A heatmap uses colors to show these correlation strengths and directions between variables.
Final Answer:
The strength and direction of relationships between variables -> Option D
Quick Check:
Heatmap = Correlation strength/direction [OK]
Hint: Heatmaps color-code relationships between variables [OK]
Common Mistakes:
Confusing correlation with frequency counts
Thinking heatmaps show raw data values
Assuming heatmaps show data order
2. Which of the following is the correct way to create a heatmap of correlations using seaborn in Python?
easy
A. sns.heatmap(data.corr())
B. sns.heatmap(data.corr)
C. sns.heatmap(corr(data))
D. sns.heatmap(data.correlation())
Solution
Step 1: Recall pandas correlation method
In pandas, correlation is calculated by calling data.corr() with parentheses.
Step 2: Use seaborn heatmap correctly
Seaborn's heatmap() accepts a matrix like the correlation matrix from data.corr().
Final Answer:
sns.heatmap(data.corr()) -> Option A
Quick Check:
Use parentheses for corr() method [OK]
Hint: Use data.corr() with parentheses inside sns.heatmap() [OK]
Common Mistakes:
Omitting parentheses after corr
Using non-existent corr() functions
Passing raw data instead of correlation matrix
3. Given the following code, what will be the output of corr_matrix?
import pandas as pd
import numpy as np
data = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [4, 3, 2, 1],
'C': [1, 3, 5, 7]
})
corr_matrix = data.corr()
medium
A. An error because data has different ranges
B. A matrix showing negative correlation between A and B, positive between A and C
C. A matrix with only positive correlations
D. A matrix showing all zeros because data is small
Solution
Step 1: Analyze data columns
Column A increases, B decreases (inverse), C increases faster than A.
Step 2: Understand correlation signs
A and B have negative correlation, A and C have positive correlation, B and C negative correlation.
Final Answer:
A matrix showing negative correlation between A and B, positive between A and C -> Option B
Quick Check:
Opposite trends = negative correlation [OK]
Hint: Opposite value trends mean negative correlation [OK]
Common Mistakes:
Assuming small data means zero correlation
Ignoring sign of correlation
Expecting errors from different value ranges
4. You wrote this code to plot a heatmap but get an error:
import seaborn as sns
import pandas as pd
data = pd.DataFrame({'X':[1,2,3],'Y':[4,5,6]})
sns.heatmap(data.corr)
What is the error and how to fix it?
medium
A. DataFrame columns must be numeric for heatmap
B. Seaborn heatmap does not accept correlation matrices
C. Missing parentheses after corr; fix by using data.corr()
D. Missing import matplotlib.pyplot as plt
Solution
Step 1: Identify the error cause
Calling data.corr without parentheses passes a method, not the correlation matrix.
Step 2: Correct the method call
Add parentheses to call the method: data.corr() to get the matrix.
Final Answer:
Missing parentheses after corr; fix by using data.corr() -> Option C
Quick Check:
Method call needs parentheses [OK]
Hint: Always use parentheses to call methods like corr() [OK]
Common Mistakes:
Passing method instead of result
Assuming heatmap can't use correlation matrix
Forgetting to import seaborn
5. You have a DataFrame with columns 'Age', 'Income', 'SpendingScore', and 'Savings'. You want to create a heatmap showing only correlations above 0.5 to focus on strong relationships. Which code snippet correctly creates this filtered heatmap?
hard
A. corr = data.corr(); sns.heatmap(corr.where(corr > 0.5))
B. corr = data.corr(); sns.heatmap(corr > 0.5)
C. corr = data.corr(); sns.heatmap(corr.filter(lambda x: x > 0.5))
D. corr = data.corr(); sns.heatmap(corr.dropna(thresh=0.5))
Solution
Step 1: Calculate correlation matrix
Use data.corr() to get all correlations.
Step 2: Filter correlations above 0.5
Use corr.where(corr > 0.5) to keep values above 0.5 and set others to NaN.
Step 3: Plot heatmap with filtered matrix
Pass the filtered matrix to sns.heatmap() to visualize only strong correlations.
Final Answer:
corr = data.corr(); sns.heatmap(corr.where(corr > 0.5)) -> Option A
Quick Check:
Use where() to filter matrix values [OK]
Hint: Use corr.where(condition) to filter heatmap values [OK]
Common Mistakes:
Using filter() which works on columns, not matrix values
Trying to index matrix with boolean mask directly
Dropping rows instead of masking values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.