Data Analysis with Python Projects - Medical Data Visualizer

For some reason i dont understand my results for the heat map are completely wrong. As far as i know the representation part of the function shouldnt chage the dataframe and neither does the bar representation. The error has to be in the clean the data section but after revising and changing the mask i havent found the bug/error. Anyway this is my code for the heatmap function:

def draw_heat_map():
    # Clean the data
    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['height'].quantile(0.025)) & 
    (df['weight'] <= df['height'].quantile(0.975))]

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

    # Generate a mask for the upper triangle
    mask = np.triu(corr)
    #Triu is a numpy method that returns the upper triangle of a matrix. It returns a new matrix with the elements below the diagonal set to 0. Tril does the same but for the lower triangle.



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

    # Draw the heatmap with 'sns.heatmap()'
    sns.heatmap(corr, linewidths=1, annot=True, square=True, mask=mask, fmt=".1f",  center=0.08, cbar_kws = {"shrink":0.5} )

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0

Challenge Information:

Data Analysis with Python Projects - Medical Data Visualizer

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Can you show the error output and link to your replit?

Or could you explain what’s wrong exactly? (share the heatmap?)

Hi, thx for responding, here’s the link to the replit: boilerplate-medical-data-visualizer (3) - Replit
and this is my heatmap.png:


From what i understand either the correlation is wrong or the cleaning data is wrong, but i’ve tried different correlation methods (pearson, kendall…) and i still get the same discrepancy.

You’re correct, there is a problem where you clean the data for the heatmap:

(df['height'] >= df['height'].quantile(0.025)) & 
    (df['height'] <= df['height'].quantile(0.975)) & 
    (df['weight'] >= df['height'].quantile(0.025)) & 
    (df['weight'] <= df['height'].quantile(0.975))]

You’re going to kick yourself but have another look at it and double check that you are comparing the correct values.

On the bright side, all of your logic was correct.

I feel so stupid right now, i think i went over that piece of code like 10 times. At least now i know that while doing so i might as well hightlight the variables with the cursor to see where they are used. Ty for the help

I didn’t see it right away either! :face_with_monocle:

Who decided to make “height” and “weight” spelled so similar? If you called them heavy and tall it would have been much easier to catch