Data Analysis with Python Projects - Medical Data Visualizer

Tell us what’s happening:

I’m trying to get the first chart (catplot) done, but I’m struggling. What’s strange is that, on my computer, the test module doesn’t complain about anything (the test runs OK), but on replit.com I get this error:

FAIL: test_bar_plot_number_of_bars (test_module.CatPlotTestCase.test_bar_plot_number_of_bars)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-medical-data-visualizer/test_module.py", line 28, in test_bar_plot_number_of_bars
    self.assertEqual(actual, expected, "Expected a different number of bars chart.")
AssertionError: 15 != 13 : Expected a different number of bars chart.

Your code so far

def draw_cat_plot():
    # 5
    df_cat = pd.melt(df,
                     id_vars='cardio',
                     value_vars=[
                         'active', 'alco', 'cholesterol', 'gluc', 'overweight',
                         'smoke'
                     ])

    # 6
    df_cat = df_cat.groupby(['cardio', 'variable',
                             'value']).size().reset_index(name='total')

    # 7
    cardio0 = df_cat[df_cat['cardio'] == 0]
    cardio1 = df_cat[df_cat['cardio'] == 1]

    # 8
    fig, axs = plt.subplots(ncols=2, figsize=(15, 5))
    sns.countplot(data=cardio0, x='variable', hue='value',
                  ax=axs[0]).set(title='cadio = 0', ylabel="total")
    axs[0].legend([], [], frameon=False)
    sns.countplot(data=cardio1, x='variable', hue='value',
                  ax=axs[1]).set(title='cadio = 1', ylabel="total")
    sns.move_legend(axs[1], "right", bbox_to_anchor=(1.15, 0.5))

    # 9
    fig.savefig('catplot.png')
    return fig

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0

Challenge Information:

Data Analysis with Python Projects - Medical Data Visualizer

I know that my plot is completely wrong: I get all ones instead of the counts. I don’t know what I did wrong. Here are my plot and the example plot:


You have a problem with the y-axis.

You create a dataframe here, with some columns ‘cardio’, ‘variable’, ‘value’ and ‘total’:

df_cat = df_cat.groupby(['cardio', 'variable', 'value'])
               .size()
               .reset_index(name='total')

Here you specify a column for the x-axis, and you specify a ylabel, but you never specify the y-axis:

sns.countplot(data=cardio0, x='variable', hue='value',
                  ax=axs[0]).set(title='cadio = 0', ylabel="total")

I notice you are also using “countplot” ? Not 100% sure if this will make a difference but it’s hard to tell how tests will react sometimes.

Convert the data into long format and create a chart that shows the value counts of the categorical features using seaborn’s catplot()

Thank you, @pkdvalis !
I’ll add the y-axis, and I’ll see if it works.
I thought that countplot is more specific and suitable in this case, because with catplot we have to specify the parameter kind.

The solution was easier than my code. I only used one catplot for bath graphics (using the parameter col, so no need to select before of plotting). Thank you for pointing out the way to go!

1 Like

Usually best to follow the word of the instructions as closely as possible. It’s hard to write tests that capture a variety of implementations and you might get unexpected results