Example in Seaborn documentation not working when I copy it into Jupyter Notebook

I am trying to insert annotations into the subplots of a Seaborn facet grid. From the Seaborn facet grid documentation we have an example containing:

for (row_val, col_val), ax in g.axes_dict.items():
    if row_val == "Lunch" and col_val == "Female":

I inserted the following into my code to see if it works. My plot object name is grid rather than g:

for ax in grid.axes_dict.items():

But it gives me the error where it claims the object in question doesn’t have the set_facecolor attribute:

AttributeError: 'tuple' object has no attribute 'set_facecolor'

I checked the object’s attributes with dir() and sure enough the attributes and methods of a matplotlib Axes object, which I was expecting, were not listed. This is what I got:

for ax in grid.axes_dict.items():

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

Am I interpreting the documentation wrong or missing something?

Without trying this out I’d hazard a guess it’s to do with calling the .items() method of what appears to be a dictionary. If you’ve covered dictionaries whilst learning python then you’d recognise .keys(), .values and .items()-Built-in Types — Python 3.9.1 documentation the last of which would return a key, value pair as a tuple.

The example code has two terms in the for loop which would unpack the tuple. If you have no need for the location then (row_val, col_val) would be unimportant but you still need the second element of that tuple.

Either use it as a tuple with ax[1] for example or assign an unimportant name to the first element within the target list for the for loop for loc, ax in ... for example.


This. I wasn’t correctly interpreting the structure of g.axes_dict.items()
Now working with:

for _, ax in grid.axes_dict.items():

Thanks, once again tgrtim you are my savior