Heatmaps and Scatter Plots: Visualizing Correlation
After completing this topic
You will be able to visualize the relationship between two variables using scatter plots and quickly grasp the correlations between multiple variables using heatmaps.
What is Correlation?
"The taller you are, the heavier you tend to be" β this is a positive correlation. As one variable increases, the other tends to increase as well.
"The higher the temperature, the fewer hot chocolate drinks are sold" β this is a negative correlation. As one variable increases, the other tends to decrease.
Correlation coefficient is a number that represents the "strength of this trend."
| Value | Meaning |
|---|---|
| +1.0 | Perfect positive correlation (both increase together) |
| +0.7 ~ +0.9 | Strong positive correlation |
| +0.3 ~ +0.7 | Weak to moderate positive correlation |
| 0 | No correlation |
| -0.3 ~ -0.7 | Weak to moderate negative correlation |
| -1.0 | Perfect negative correlation (move in opposite directions) |
Scatter Plot: Viewing the Relationship Between Two Variables
import seaborn as snsimport matplotlib.pyplot as plt
sns.scatterplot(data=df, x='experience', y='salary')plt.title('Experience vs. Salary')plt.show()If the points cluster towards the upper right, it indicates a positive correlation. If they cluster towards the lower right, it indicates a negative correlation. If the points are scattered all over, it indicates no correlation.
Adding a Trend Line
sns.regplot(data=df, x='experience', y='salary', scatter_kws={'alpha': 0.5}, line_kws={'color': 'red'})regplot draws both a scatter plot and a regression line. You can visually assess the strength of the correlation by observing how well the line passes through the points.
Correlation Matrix: Multiple Variables at Once
If you have only two variables, a single scatter plot is sufficient. But what if you have 10? You would need 45 scatter plots to view all combinations. This is where a correlation matrix comes in handy.
# Calculate the correlation coefficient matrixcorr = df[['salary', 'experience', 'age', 'projects']].corr()print(corr)salary experience age projects
salary 1.000 0.850 0.620 0.430
experience 0.850 1.000 0.780 0.350
age 0.620 0.780 1.000 0.120
projects 0.430 0.350 0.120 1.000It's a numerical matrix, so it's not easy to grasp at a glance. What we do is convert it into colors, which is called a heatmap.
Heatmap: Visualizing the Correlation Matrix
plt.figure(figsize=(8, 6))sns.heatmap(corr, annot=True, # Display numbers in cells fmt='.2f', # Two decimal places cmap='coolwarm', # Red (positive) to blue (negative) vmin=-1, vmax=1, # Fix color range square=True, # Square cells linewidths=0.5) # Lines between cellsplt.title('Correlation Between Variables')plt.tight_layout()plt.show()How to read a heatmap:
- Red cells β strong positive correlation (both increase together)
- Blue cells β strong negative correlation (move in opposite directions)
- White cells β no correlation (independent)
- Diagonal β always 1.0 (correlation with itself)
Real-World Interpretation Cautions
Correlation β Causation
"Ice cream sales" and "number of drowning accidents" have a high correlation. However, ice cream does not cause people to drown. Both are influenced by a hidden variable called "summer (temperature)."
Correlation simply indicates that "they move together." It is different from causation, which means "one causes the other."
Non-Linear Relationships
The correlation coefficient (Pearson) only measures linear relationships. A U-shaped relationship (e.g., moderate stress is good for performance, but too much stress is bad) may result in a correlation coefficient close to 0. Always visually confirm using a scatter plot.
pairplot: Scatter Plot Matrix
If you have a small number of variables, pairplot is useful because it shows all combinations of scatter plots in a grid:
sns.pairplot(df[['salary', 'experience', 'age', 'projects']], diag_kind='kde')plt.show()- Diagonal: Distribution of each variable (histogram or KDE)
- Rest: Scatter plot of all variable pairs
If you have 5 or more variables, the graph becomes too small to read. It is more efficient to look at the overall trend with a heatmap and then zoom in on specific pairs with a scatter plot.
Key Takeaway
Scatter plots show the relationship between two variables, while heatmaps show the correlations between multiple variables at a glance. The correlation coefficient ranges from -1 (perfect negative correlation) to +1 (perfect positive correlation) β 0 means no linear relationship. Correlation β Causation β don't just look at the numbers, always check with a scatter plot.