I’m working on the project and have my first plot basically correct, but for some reason the functions output two figures.
Catplot:
# 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'])
df_cat['total'] = 0
# 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']).count().reset_index()
# Draw the catplot with 'sns.catplot()'
bar = sns.catplot(x='variable', y = 'total', hue = 'value', col = 'cardio', data = df_cat, kind = 'bar')
fig = bar.fig
# Do not modify the next two lines
fig.savefig('catplot.png')
return fig
This outputs:
I only want one, obviously, but get two.
Next is the heat map:
def draw_heat_map():
# Clean the data
#df_heat = df.drop("bmi", axis = 1)
df_heat = df
df_heat = df_heat[df_heat["ap_lo"] <= df_heat["ap_hi"]]
df_heat = df_heat[df_heat["height"] >= df_heat["height"].quantile(0.025)]
df_heat = df_heat[df_heat["height"] <= df_heat["height"].quantile(0.975)]
df_heat = df_heat[df_heat["weight"] >= df_heat["weight"].quantile(0.025)]
df_heat = df_heat[df_heat["weight"] <= df_heat["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))
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize = (10, 12))
# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr, mask = mask, vmax = 0.3, center = 0, annot = True,
square = True, linewidths=0.5, cbar_kws={"shrink": 0.5})
# Do not modify the next two lines
fig.savefig('heatmap.png')
return fig
This outputs:
Do you know how I edit it to just put out one figure? Thanks.