【发布时间】:2015-05-25 21:44:48
【问题描述】:
我想用labels 和colors 构建自己的Scatterplot Matrix。我喜欢这个例子:Scatterplot Matrix。
我有一个问题。我不明白,如何将数据从numpy matrix 加载到seaborn dataframe。
data_resc = np.random.rand(150,2)
sns.set()
df = DataFrame(data_resc)
sns.pairplot(df, hue="species", size=2.5)
sns.plt.show()
这段代码sn-p有错误:
Traceback (most recent call last):
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3687)
File "pandas\hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7192)
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "file.py", line 69, in <module>
main()
File "file.py", line 64, in main
sns.pairplot(df, hue="species", size=2.5)
File "C:\Python34\lib\site-packages\seaborn\linearmodels.py", line 1720, in pairplot
size=size, aspect=aspect, dropna=dropna, **grid_kws)
File "C:\Python34\lib\site-packages\seaborn\axisgrid.py", line 857, in __init__
hue_names = np.unique(np.sort(data[hue]))
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1780, in __getitem__
return self._getitem_column(key)
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1787, in _getitem_column
return self._get_item_cache(key)
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1068, in _get_item_cache
values = self._data.get(item)
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2849, in get
loc = self.items.get_loc(item)
File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1402, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3807)
File "pandas\index.pyx", line 156, in pandas.index.IndexEngine.get_loc (pandas\index.c:3744)
KeyError: 'species'
【问题讨论】:
-
您收到了一个关键错误,因为您在没有指定列名的情况下盲目地创建了一个数据框,在这种情况下,一些默认值从 0 开始提供。如果您这样做了
df = DataFrame(columns=['species', 'value'], data=data_resc),那么绘图代码将工作 -
在示例中,您链接到加载 df 的行:
df = sns.load_dataset("iris")这将设置列名称“sepal_length”、“sepal_width”、“petal_length”、“petal_width”,这就是散点图起作用的原因.所以你需要自己提供列名 -
@EdChum,它有效,但有两个问题。如果我重命名列
species,则会发生错误。其次,我不知道如何设置我的数据类别。换句话说,我的一部分数据是class 1,第二部分是class 2等等。 -
你应该知道你的数据代表什么,我不能告诉你
标签: python numpy pandas dataframe seaborn