首页 > 解决方案 > Seaborn FacetGrid KDE 值错误

问题描述

sns.FacetGrid在 seaborn 的几个核密度图( )中做一个平面网格( sns.kdeplot)并运行这个代码:

import pandas as pd
import seaborn as sns
data = sns.load_dataset('exercise')
g = sns.FacetGrid(data=data, col='diet')
g.map(sns.kdeplot,'pulse',hue='kind')

导致此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/MislabelUnc/compare/debugsns.py in 
      11 g = sns.FacetGrid(data=data, col='diet')
----> 12 g.map(sns.kdeplot,'pulse',hue='kind')

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/axisgrid.py in map(self, func, *args, **kwargs)
    681 
    682             # Draw the plot
--> 683             self._facet_plot(func, ax, plot_args, kwargs)
    684 
    685         # Finalize the annotations and layout

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    773             plot_args = []
    774             plot_kwargs["ax"] = ax
--> 775         func(*plot_args, **plot_kwargs)
    776 
    777         # Sort out the supporting information

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/distributions.py in kdeplot(x, y, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, cbar, cbar_ax, cbar_kws, ax, weights, hue, palette, hue_order, hue_norm, multiple, common_norm, common_grid, levels, thresh, bw_method, bw_adjust, log_scale, color, fill, data, data2, **kwargs)
   1694     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
   1695 
-> 1696     p = _DistributionPlotter(
   1697         data=data,
   1698         variables=_DistributionPlotter.get_semantics(locals()),

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/distributions.py in __init__(self, data, variables)
    107     ):
    108 
--> 109         super().__init__(data=data, variables=variables)
    110 
    111     @property

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in __init__(self, data, variables)
    602     def __init__(self, data=None, variables={}):
    603 
--> 604         self.assign_variables(data, variables)
    605 
    606         for var, cls in self._semantic_mappings.items():

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in assign_variables(self, data, variables)
    665         else:
    666             self.input_format = "long"
--> 667             plot_data, variables = self._assign_variables_longform(
    668                 data, **variables,
    669             )

~/anaconda3/envs/plot/lib/python3.9/site-packages/seaborn/_core.py in _assign_variables_longform(self, data, **kwargs)
    900 
    901                 err = f"Could not interpret value `{val}` for parameter `{key}`"
--> 902                 raise ValueError(err)
    903 
    904             else:

ValueError: Could not interpret value `kind` for parameter `hue`

我不知道是什么原因造成的,因为

sns.kdeplot(data=data, x='pulse', hue='kind')

工作得很好。

我究竟做错了什么?

标签: pythonpandasseaborn

解决方案


子图按 DIET 列划分,按 KIND 列的分解由分面网格指定。

import pandas as pd
import seaborn as sns
data = sns.load_dataset('exercise')
g = sns.FacetGrid(data=data, col='diet', hue='kind')
g.map(sns.kdeplot,'pulse')

在此处输入图像描述


推荐阅读