Can't complete project

Tell us what’s happening:
I’ve spent 2 days trying to complete project with Medical Data Visualizer, but vainly.
I have three fails which I can’t fix:

  1. Saving catplot (two fails connected with it)
    Unfortunately in the course wasn’t provided any information about data visualization. So I am stuck with Catplot, which is always saved as blank plot, though running same code in jupyter lab gives nice plot with data.
  2. Saving heatmap
    I am sure I’ve done everything right, but in some cells of heatmap there negative zeroes instead of positive as it is given in tester

Please help

Your code so far
First plot:

# 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'.
    df_cat = df.melt(value_vars = ['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight'])


    # 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.melt(id_vars = ['cardio'], value_vars = ['cholesterol', 'gluc', 'smoke', 'alco', 'active', 'overweight'])

    # Draw the catplot with 'sns.catplot()'
    fig, ax =  plt.subplots()
    ax = sns.catplot(x='variable',
                col='cardio',
                hue = 'value',
                data=df_cat,
                kind="count")


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

Second plot

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['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(corr)
    
    # Set up the matplotlib figure
    fig, ax = plt.subplots()

    # Draw the heatmap with 'sns.heatmap()'
    ax = sns.heatmap(corr, annot = True, mask=mask, fmt = '.1f')


    # 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/99.0.4844.84 Safari/537.36

Challenge: Medical Data Visualizer

Link to the challenge:

I’ve edited your post 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 (’).

For a start, why even call plt.subplots() if you are not doing anything with it?
Then please concern yourself with the documentation on how to use the two together.

Rhe reason why I call plt.subplots() is that it is the only fubction, whuch gives ‘fig’ and ‘ax’ output. In a blank project there is line for the second plot like

fig, ax = None

I found that these two variables are commonly used with plt.subplots. The second plot is saved without errors. Before that my first plot was difined as just:

fig = sns.catplot(x='variable',
                col='cardio',
                hue = 'value',
                data=df_cat,
                kind="count")

And it returned two errors while testing. Firstly it doesn’t have method .get_children, which is used in testing module (error1), secondly it doesn’t have method get_xlabel whixh is also used.
So I decided to change the first one like the second. Now I have no errors, but plot is never saved.

I’ve read the documentation and stackoverflow and a lot of other sites, but couldn’t find the right way to complete this task. I am novice and studying Python for a couple of weeks, surely I could miss something. But right now I have no idea what to do and where to find answers.

Those are not random variables which you can just change…
The error happens because seaborn returns a facetgrid object, but the test wants the actual matplot figure wich is saved in the .figure attribute of the facetgrid :wink:

You don’t get errors becauce you save the plot in a local variable that’s not returned. What is returned is the empty figure you created, which is still empty.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.