Medical Data Visualizer different values in heat map

Medical Data Visualizer

Hi,

I am not able to resolve a silly issue in the above project.
My code is done already, however I have issue with the last test.
The self.ax.get_default_bbox_extra_artists() method returns an object with several Text object and at the end three different matplotlib object.
The next line is create the actual list with the data values.
Every single values are equal with the expected list, except the last three value.

In the expected list the last three elements are empty string: ""

In the actual list these empty strings missing.

I cannot to find what is the solution, and more worst I do not find any mistake in the code…

this is the affected code snippet:
def draw_heat_map():

    # Clean the data

    # https://cmdlinetips.com/2018/02/how-to-subset-pandas-dataframe-based-on-values-of-a-column/

    pressure_filter = df["ap_lo"] <= df["ap_hi"]

    height_filter = (df["height"] >= df["height"].quantile(0.025)) & (df["height"] <= df["height"].quantile(0.975))

    weight_filter = (df["weight"] >= df["weight"].quantile(0.025)) & (df["weight"] <= df["weight"].quantile(0.975))

    df_heat = df[pressure_filter & height_filter & weight_filter]

    # Calculate the correlation matrix

    # https://seaborn.pydata.org/examples/many_pairwise_correlations.html

    corr = df_heat.corr()

    # Generate a mask for the upper triangle

    # https://seaborn.pydata.org/generated/seaborn.heatmap.html

    mask = np.triu(np.ones_like(corr, dtype = bool))

    # Set up the matplotlib figure

    fig, ax = plt.subplots(figsize=(11, 9))

    # Draw the heatmap with 'sns.heatmap()'
    # https://seaborn.pydata.org/generated/seaborn.heatmap.html
    sns.heatmap(

        corr,

        mask = mask,

        linewidths = 0.5,

        annot = True,            # If True, write the data value in each cell.

        fmt = ".1f",             # String formatting code to use when adding annotations.

        center = 0.08,

        cbar_kws = {

            "shrink": 0.5        # half the size if colorbar

        }

    )

    # Do not modify the next two lines

    fig.savefig('heatmap.png')

    return fig

Is anyone has an idea?
The full code on Repl.

lendoo

@lendoo so delete the 3 empty strings at the end of the expected list located in test_module.py :man_shrugging:t4:

Hi,

Based on this post and your suggestion I modified the test_heat_map_values() method in the test file:

test_heat_map_values()
    def test_heat_map_values(self):

        actual = [text.get_text() for text in self.ax.get_default_bbox_extra_artists() if isinstance(text, mpl.text.Text)]

        expected = ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.1', '0.0', '0.2', '0.1', '0.0', '0.1', '-0.0', '-0.1', '0.1', '0.0', '0.2', '0.0', '0.1', '-0.0', '-0.0', '0.1', '0.0', '0.1', '0.4', '-0.0', '-0.0', '0.3', '0.2', '0.1', '-0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.2', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.0', '-0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '-0.0', '0.2', '0.1', '0.3', '0.2', '0.1', '-0.0', '-0.0', '-0.0', '-0.0', '0.1', '-0.1', '-0.1', '0.7', '0.0', '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1', '', '', '']

        

        # check the version of matplotib:

        ver = int(mpl.__version__.replace(".", ""))

        if ver >= 330:

            expected = expected[:-3]

            

        self.assertEqual(actual, expected, "Expected differnt values in heat map.")

I did not tested the versions. If anyone knows the exact version where the return of the matplotlib..ax.get_default_bbox_extra_artists() method been changed, then have to modify the version number from 330 to the exact version.

1 Like

I had two problems with the values in the heat map. First, the problem was I wasn’t returning any values and therefore inside the boxes of my heat map were empty. To fill them like they did in the example figure, I had to set “annot” parameter to “True” inside the sns.heatmap() function. Second, the decimal places were not matching with the test module. Therefore, I set the “fmt” parameter again inside the sns.heatmap() function to “.1f” so that it returned only 1 decimal. This solved my problem with the heatmap.