构面网格不适用于分类变量

时间:2018-08-17 02:25:54

标签: python python-3.x seaborn

尝试使用Seaborn创建Facet网格时出现错误。我有3个类别变量:性别,星期几,颜色。我想了解每个类别中所有值之间的直接关联。

性别:女,男
星期几:周二,周二,周三,周四,周五,周六,周日
颜色:红色,绿色。

g = sns.FacetGrid(tips, col="Gender",  row="Color")
g = g.map(plt.hist, "Day of the Week")
display()

Get an error:
KeyError-Traceback (most recent call last)
<command-206114> in <module>()
      2 tips = sns.load_dataset("tips")
      3 
----> 4 g = sns.FacetGrid(tips, col="Gender",  row="Color")
      5 g = g.map(plt.hist, "Day of the week")
      6 display()

/databricks/python/lib/python3.5/site-packages/seaborn/axisgrid.py in __init__(self, data, row, col, hue, col_wrap, sharex, sharey, size, aspect, palette, row_order, col_order, hue_order, hue_kws, dropna, legend_out, despine, margin_titles, xlim, ylim, subplot_kws, gridspec_kws)
    240             row_names = []
    241         else:
--> 242             row_names = utils.categorical_order(data[row], row_order)
    243 
    244         if col is None:

/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2057             return self._getitem_multilevel(key)
   2058         else:
-> 2059             return self._getitem_column(key)
   2060 
   2061     def _getitem_column(self, key):

/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2064         # get column
   2065         if self.columns.is_unique:
-> 2066             return self._get_item_cache(key)
   2067 
   2068         # duplicate columns & possible reduce dimensionality

/databricks/python/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1384         res = cache.get(item)
   1385         if res is None:
-> 1386             values = self._data.get(item)
   1387             res = self._box_item_values(item, values)
   1388             cache[item] = res

/databricks/python/lib/python3.5/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3541 
   3542             if not isnull(item):
-> 3543                 loc = self.items.get_loc(item)
   3544             else:
   3545                 indexer = np.arange(len(self.items))[isnull(self.items)]

/databricks/python/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
   2134                 return self._engine.get_loc(key)
   2135             except KeyError:
-> 2136                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2137 
   2138         indexer = self.get_indexer([key], method=method, tolerance=tolerance)


KeyError: 'Color'

有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

变量“颜色”是这里的问题。我可能拼错了。如果您要绘制两个分类变量,请尝试使用条形图。