Python Forum
Heatmaps are not populating all values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Heatmaps are not populating all values
#1
Hi everyone,

As the title implies, I am trying to create heatmaps but not all values are being shown.

This is on a Jupyter Notebook.

[Image: image.png]

# Show correlation heatmap and matrix
df_corr = df[['floor_area_sqm', 'resale_price', 'lease_commence_date']]

# Calculate the correlation matrix
corrmat = df_corr.corr()

# Show the heatmap
import seaborn as sns
import matplotlib.pyplot as plt

sns.heatmap(corrmat, annot=True)

plt.show()
[Image: image.png]

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder

# Create a copy with only the columns we need
df_corr = df[['floor_area_sqm', 'resale_price', 'lease_commence_date', 'storey_range']].copy()

# Convert storey_range to numeric using LabelEncoder
le = LabelEncoder()
df_corr['storey_range'] = le.fit_transform(df_corr['storey_range'])

# Ensure all columns are numeric
df_corr = df_corr.apply(pd.to_numeric, errors='coerce')

# Drop rows with any NaN values to ensure clean data
df_corr = df_corr.dropna()

# Compute correlation matrix
corrmat = df_corr.corr(method='pearson')

# Debugging: Print correlation matrix and its shape
print("\nCorrelation matrix shape:", corrmat.shape)
print("\nCorrelation matrix:")
print(corrmat)

# Ensure there are no NaN values in the correlation matrix
if corrmat.isnull().values.any():
   print("NaN values found in the correlation matrix!")
else:
   print("No NaN values in the correlation matrix.")

# Modified heatmap code
plt.figure(figsize=(12, 10))
sns.heatmap(corrmat, 
           annot=True)   # Add this to ensure annotations are visible

# Ensure the full matrix is displayed
plt.subplots_adjust(bottom=0.15)

# Rotate x-axis labels for better readability
plt.xticks(rotation=45, ha='right')
plt.yticks(rotation=0)

# Set title and adjust layout
plt.title('Correlation Matrix Heatmap')
plt.tight_layout()
plt.show()
I tried debugging it and the values do seem to be calculated correctly:

[Image: image.png]

Any help is much appreciated! :)
likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  populating a list rwahdan 2 2,719 Jun-23-2021, 07:20 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020