Medical Data Visualizer: Getting errors in working graphs

Hello, I’ve been working the last couple of days on this task. However, I’ve reached a point where I’m completely lost on what to do to pass the tests. I’ve tested my code in jupyter lab to check the graphs, I’ve got two versions of the catplot, both identical to the example, but the test returns.

Regarding the heatmap, I’ve got the values right, but I can’t seem to get the color bar to match that of the example. Thanks in advance for the help.

AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_children’

This is my code so far:

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=[‘active’, ‘alco’, ‘cholesterol’, ‘gluc’, ‘overweight’, ‘smoke’])

# 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_catfilter = df_cat.groupby(['cardio','variable','value']).size().reset_index(name='total')


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


# Do not modify the next two lines
fig.savefig('catplot.png')
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[‘height’] <= df[‘height’].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(figsize=(9,9))

# Draw the heatmap with 'sns.heatmap()'
sns.heatmap(corr, mask=mask, square=True, cmap="icefire", linewidth=0.3, cbar_kws={"shrink": 0.5},
                        annot=True, fmt='.1f')
  • List item

Do not modify the next two lines

fig.savefig('heatmap.png')
return fig

Challenge: Medical Data Visualizer

I’m having the same problem, mine give me this error

ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-1/test_module.py”, line 26, in test_bar_plot_number_of_bars
actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rectangle)])
AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_children’

======================================================================
ERROR: test_line_plot_labels (test_module.CatPlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-medical-data-visualizer-1/test_module.py”, line 13, in test_line_plot_labels
actual = self.ax.get_xlabel()
AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_xlabel’

For the heat map color scale to be the same, try setting the maximum and minimum scale with vmax=0.24 and vmin=-0.08.

Check this post, it worked for me. Medical Data Visualizer Confusion - #52 by gabriellguedes16

1 Like

Thanks! Worked as intended.

I get the same colors. Regardless I’m getting this error:

First differing element 9:
‘0.2’
‘0.3’

Diff is 992 characters long. Set self.maxDiff to None to see it. : Expected differnt values in heat map.

I’m checking the values and they look identical, any ideas?

This is in the correlation matrix check right? Maybe the problem is in the cleaning, when you filter the outliers.

Sorry I didn’t see that you put the code for the filtering and it is correct, I checked my project and remembered that I was getting a similar error, I was rounding the correlation matrix. Try passing to the heat map the parameter fmt = “1.f” to set the number of decimal places that the annot shows.

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