FCC Medical Data Visualizer

Hello. I’ve got stuck in plotting chart with seaborn.catplot(). I’m using the code below but both tests return error.

The code is hosted in repl.it.

Anyone can help on this? Thanks in advance.

fig = sns.catplot(
x = ‘variable’,
y = ‘total’,
col = ‘cardio’,
hue = ‘value’,
kind = ‘bar’,
data = df_cat
fig.set_axis_labels(‘variable’, ‘total’)

ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)

Traceback (most recent call last):
File “/home/runner/fcc-medical-data-visualizer/test_module.py”, line 26, in test_bar_plot_n
actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rec
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/fcc-medical-data-visualizer/test_module.py”, line 13, in test_line_plot_labels
actual = self.ax.get_xlabel()
AttributeError: ‘numpy.ndarray’ object has no attribute ‘get_xlabel’

catplot() returns FacetGrid object. fig attribute can be used to access figure in the FacetGrid.


With the point you indicated I was able to access the labels with fig.fig.axes[0].get_xlabels() and fig.fig.axes[0].get_ylabels(). I removed fig.set_xlabels() as this isn’t need.

Now I have a draw_cat_plot() function that returns a FacetGrid object as requested in the exercise and then I turned attention to the test cases provided for evaluating the challenger. It was initialized as follows:

# the test case
class CatPlotTestCase(unittest.TestCase):
    def setUp(self):
        self.fig = medical_data_visualizer.draw_cat_plot()
        self.ax = self.fig.axes[0]
    def test_line_plot_labels(self):
        actual = self.ax.get_xlabel()
        expected = "variable"
        self.assertEqual(actual, expected, "Expected line plot xlabel to be 'variable'")
        actual = self.ax.get_ylabel()
        expected = "total"
        self.assertEqual(actual, expected, "Expected line plot ylabel to be 'total'")
        # more code after this point

When test_line_plot_labels() calls self.ax.get_xlabel() it raises an AttributeError. It solves by making self.ax = self.fig.fig.axes[0] in setUp() method.

You can still make function to return the figure, what doesn’t require changing test.

g = sns.catplot(...)
fig = g.fig

This is much better. Thanks again.

A remark: I stumbled across the same error, I did someting like this:
fig_wrong = sns.catplot(...) # WRONG: not figure, but FacetGrid

The ugly thing is: that FacetGrid-Object has also a savefig() method!
The png-file was created correctly, so I thought: Everything is fine!
But the test_module did not run.

With that sanity-solution it’s really working, also in test_module.py

g = sns.catplot(...)
fig = g.fig

So thanks!

1 Like