I have written my program for this challenge, however when I run it with the test module I am getting the following errors:
EEEE
======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\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 "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\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'
======================================================================
ERROR: test_heat_map_labels (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 34, in setUp
self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object does not support indexing
======================================================================
ERROR: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 34, in setUp
self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object does not support indexing
----------------------------------------------------------------------
Ran 4 tests in 2.290s
FAILED (errors=4)
To my mind, which is still learning python, these look like errors with the python code in the test module rather than with anything I have done (i.e. they are not assertion errors).
I suspect the explanation is one of either
a) there is a problem with my code which is provoking these errors
or
b) I am not using the correct version of numpy.
If anyone has seen this before or is able to provide me with some help or direction it would be much appreciated.
For the catplot function I am using the variable name fig because that is what is provided in the challenge template. It is just a variable name though, for example:
<seaborn.axisgrid.FacetGrid object at 0x000001BB762767F0>
So my function is returning a FacetGrid object but if you look at the errors for the catplot function (errors on lines 13 and 26 above), it seems to think self.ax is a numpy.nparray whereas I’d expect it to be a matplotlib.axes object (because self in the test case is the fig that I have previously created).
To confirm this, I looked at the test module. This code is from the test module:
It’s not as expected, tests expect figure object. Notice that you aren’t actually printing type of the self.ax, but just str representation of it. Using type(self.ax) instead likely would reveal numpy.ndarray.
You are correct, using type(self.ax) returned numpy.ndarray
You say that the tests expect figure object, however the medical_data_visualizer.py template states that we should use the sns.catplot() and sns.heatmap() methods which, as you have previously stated, return FacetGrid and ax. This doesn’t seem consistent to me but I’ll keep looking at it.
@sanity is correct. If you dig through the seaborn and matplotlib documentation, you’ll find that many (not all) seaborn methods return a FacetGrid, which contains a matplotlib Figure object, which in turn holds an Axes object.
Looking at my code, I access the Figure in the FacetGrid returned by sns.catplot() by using its .fig property, and then you can get to the Axes objects by the Figure object’s .axes[...] list.
All the tests are checking things on the Axes object, and the tests get that object by calling your function, and assuming it returns a matplotlib Figure, from which it then gets the appropriate Axes object, and runs the tests. So if you return a FacetGrid when the test assumes a Figure, you get these errors because the FacetGrid has a numpy array stored at ax, and when self.ax.whatever() gets called, it’s not there because it is a numpy array and not a matplotlib Axes. Return the Figure from the FacetGrid once you are finished processing it and you should be in better shape although I found I needed to activate the Axes object on a Figure to get some tests to work.
Traceback (most recent call last):
File “c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py”, line 47, in test_heat_map_values
self.assertEqual(actual, expected, “Expected differnt values in heat map.”)
AssertionError: Lists differ: ['0.0[607 chars] ‘0.2’, ‘0.1’, ‘0.1’, ‘-0.0’, ‘0.0’, ‘-0.0’, ‘0.1’] != ['0.0[607 chars] ‘0.2’, ‘0.1’, ‘0.1’, ‘-0.0’, ‘0.0’, ‘-0.0’, ‘0.1’, ‘’, ‘’, ‘’]
Second list contains 3 additional elements.
First extra element 91:
‘’
Diff is 989 characters long. Set self.maxDiff to None to see it. : Expected differnt values in heat map.
I had a look at previous posts for this issue and they suggested that I set the matplotlib version to “3.2.2” in the repl.it repository poetry.lock file. I don’t think this will help me as I am running the tests in VS Code and I am still seeing this issue, my python environment is currently using matplotlib version 3.3.2.
Edit. I have resolved this as well, had to revert matplotlib back to 3.1.3.