# Data Analysis with Python Projects - Medical Data Visualizer

Hi,

### Tell us what’s happening:

My code failes the test_heat_map_value because because my correlation matrix seems to be all wrong. But I don’t get why.

``````import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Import data
#Add an overweight column to the data.
# To determine if a person is overweight, first calculate their BMI by dividing their weight in kilograms by the square of their height in meters.
df['BMI']=df['weight']/(df['height']/100)**2
# If that value is > 25 then the person is overweight. Use the value 0 for NOT overweight and the value 1 for overweight.
df['overweight'] = (df['BMI']>25).astype(int)
df.drop(columns=['BMI'], inplace=True)
# Normalize data by making 0 always good and 1 always bad. If the value of 'cholesterol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df['cholesterol'] = (df['cholesterol']>1).astype(int)
df['gluc'] = (df['gluc']>1).astype(int)

#df.drop(columns=['BMI'], inplace=True)
# Draw Categorical Plot
def draw_cat_plot():
# Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
#define the list of categories
cats = ['cholesterol', 'gluc', 'smoke', 'alco', 'active','overweight']
df_cat = pd.melt(df, id_vars=['id'], value_vars = cats, var_name='variable', value_name='value')
df_cat['variable']= pd.Categorical(df_cat['variable'])

#df_cardio = df['id', 'cardio']
df_cat = pd.merge(df_cat, df.loc[:, ['id', 'cardio']], on= 'id', how = 'left')

# Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the columns for the catplot to work correctly.

df_cat = df_cat.groupby(['cardio', 'variable', 'value'])
groupe_counts = df_cat.size().reset_index(name='count')

# Define the order of categories for the 'value' column
value_order = sorted(groupe_counts['value'].unique())

# Convert the 'value' column to categorical with the specified order
groupe_counts['value'] = pd.Categorical(groupe_counts['value'], categories=value_order)

# Draw the catplot with 'sns.catplot()'
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(10, 8))
# Use sns.catplot to represent the data
ax = sns.catplot(x='variable', y='count', kind='bar', hue='value', col='cardio', data=groupe_counts)
#catplot.set_ylabels('total')
ax.set_ylabels('total')
# Get the figure for the output
# Get the current figure
fig = plt.gcf()

# Do not modify the next two lines
fig.savefig('catplot.png')
return fig

# Draw Heat Map
def draw_heat_map():
# Clean the data
# Filter out incorrect data
# # where diastolic pressure is higher than systolic pressure
filtered_df = df[df['ap_lo'] <= df['ap_hi']]
# # height is less than the 2.5th percentile (Keep the correct data with (df['height'] >= df['height'].quantile(0.025)))
#filtered_df = filtered_df[filtered_df['height'] >= filtered_df['height'].quantile(0.025)]
# # height is more than the 97.5th percentile
#filtered_df = filtered_df[filtered_df['height'] <= filtered_df['height'].quantile(0.975)]
# # weight is less than the 2.5th percentile
#filtered_df = filtered_df[filtered_df['weight'] >= filtered_df['weight'].quantile(0.025)]
# # weight is more than the 97.5th percentile
#filtered_df = filtered_df[filtered_df['weight'] <= filtered_df['weight'].quantile(0.975)]
#shorter way
df_heat = df[(df['ap_lo'] <= df['ap_hi']) &
(df['height'] >= df['height'].quantile(0.025)) &
(df['height'] <= df['height'].quantile(0.975)) &
(df['weight'] >= df['weight'].quantile(0.025)) &
(df['weight'] <= df['weight'].quantile(0.975))]

# Calculate the correlation matrix
corr = df_heat.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool)) #np.ones_like(corr, dtype=bool): creates a new NumPy array filled with ones,
#having the same shape and data type as the corr array.
#dtype=bool argument specifies that the data type of the new array should be boolean (True or False).
#np.triu(...): is used to extract the upper triangle of an array and set the elements below the diagonal to zero.
#By default, it includes the diagonal as well. However, the ones_like array already contains ones in the upper triangle,
# so applying np.triu() doesn't change the array, but it serves to mask out the lower triangle.

# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(10, 8))

# Draw the heatmap with 'sns.heatmap()'
#plt.show()

# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0`

### Challenge Information:

Data Analysis with Python Projects - Medical Data Visualizer

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

```````ax = sns.heatmap(corr, mask=mask, annot=True, fmt=".2f", linewidths=.5, ax=ax, annot_kws={"size": 8})`
I switched fmt=“.2f” to fmt=“.1f” in the `sns.heatmap` function call.