Medical Data Visualizer Confusion

try this
df_cat = df_cat.groupby([‘cardio’,‘variable’, ‘value’], as_index = False).size().rename(columns={‘size’:‘total’})

1 Like

Yes, it works perfectly fine. I wonder what’s the purpose of creating a data-frame which is grouped by cardio…

Doesn’t work for me, I get wired plot in my legend there are two 1 and two 0

I got this solution for the catplot, and it worked!

df_cat = pd.melt(df, id_vars = "cardio", value_vars = ["active", "alco", "cholesterol", "gluc", "overweight", "smoke"])

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

I have a question, why is gluc and cholesterol ‘normalised’ in a different way? Why can’t cholesterol be ‘normalised’ in the same way as gluc is? Thank you!

Like many of you, I am getting plots that look correct but failing the tests. It didn’t seem like anyone completely solved this.

It wasn’t working for me either…I was plotting the catplot just like others taught before but, although I was getting the graphs right, it was failing the tests:
fig = sns.catplot(x = "variable", hue = "value", col = "cardio", data = df_cat, kind = "count")
The point that others explained was:

  1. The catplot as it is returns a FacetGrid object, thus, you need to get the figure attribute to pass as the return of the “draw_cat_plot” function. Kind of like this:
    g = sns.catplot(x = "variable", hue = "value", col = "cardio", data = df_cat, kind = "count")
    fig = g.fig
    return fig

  2. Finally, you need to set the y-label to ‘total’ instead of ‘count’ to match the testing code. And for that, you can simply say :
    img.set_axis_labels(“variable”, “total”)

And that is it. My catplot results worked fine after this.