Graphs Not Showing Up - Python

Can someone assist me check why there are no graphs plotted? It’s all about exception handling but…I feel the code below is somewhat okay but …may need fixing?

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

# Import data
df = pd.read_csv('medical_examination.csv')

# Add 'overweight' column
df['overweight'] = (df['weight']/(df['height']/100)**2).apply(lambda x : 1 if x >25 else 0)

# Normalize data by making 0 always good and 1 always bad. If the value of 'cholestorol' or 'gluc' is 1, make the value 0. If the value is more than 1, make the value 1.
df.loc[df['cholesterol']==1, 'cholesterol'] = 0
df.loc[df['cholesterol']==1, 'cholesterol'] = 1

df.loc[df['gluc']==1, 'gluc'] = 0
df.loc[df['gluc']==1, 'gluc'] = 1

# 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 = pd.melt(df, id_vars = ['cardio'], 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 collumns for the catplot to work correctly.

    df_cat['total'] = 1
    df_cat = df_cat.groupby(['cardio','variable', 'value'], as_index = False).count()

    # Draw the catplot with 'sns.catplot()'
    fig = sns.catplot(
      x = 'variable',
      y = 'total',
      kind = 'bar',
      col = 'cardio',
      data = df_cat

    # Do not modify the next two lines
    return fig

# Draw Heat Map
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 = sns.heatmap(df_heat.corr(), annot = True)
    corr = df_heat.corr()

    # Generate a mask for the upper triangle
    # NumPy array creation: triu() function
    # Upper triangle of an array. The triu() function is used to get a copy of a matrix with the elements below the k-th diagonal zeroed.Feb 26, 2020
    mask = np.triu(corr)

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

    # Draw the heatmap with 'sns.heatmap()'
    sns.heatmap(corr, linewidths=1, mask=mask, vmax=.3, center=0.09,square=True)

    # Do not modify the next two lines
    return fig

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

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 (’).

Thank you.

1 Like