Seaborn Creating Empty Axes Placeholder for Non-Existent Data

Question:

I am trying to understand why I am getting empty axes for data that is not in an indexed dataframe. I am loading a dataframe, then indexing to only include data where the ‘day’ column is either ‘Thur’ or ‘Fri’, but for some reason when using that indexed dataframe I still get empty axes for ‘Sat’ and ‘Sun’. How does seaborn/relplot even know about those values when I thought they would be non-existent in the dataframe that I passed into relplot?

Executing the below code

import seaborn as sns
tips = sns.load_dataset('tips')
dataSubset=tips[tips['day'].isin(['Thur', 'Fri'])]
g = sns.relplot(x="total_bill", y="tip", row='day', data=dataSubset, facet_kws={'margin_titles': True}, height=2, aspect=1.5)

Produces
Output Figure

I don’t understand why there are empty axes for ‘Sat’ & ‘Sun’ or where in the dataframe those entries are still registered. Any insight would be appreciated.

Versions

  • Python – 3.9.5
  • Seaborn – 0.11.2
  • Pandas – 1.2.5
Asked By: Thurbs

||

Answers:

As user BigBen hints in the comments, the day column is a category dtype and subsetting does not remove unused categories. The following addition will fix:

dataSubset['day'] = dataSubset.day.cat.remove_unused_categories()
Answered By: Derek Croote
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.